I’m creating application with Codeigniter and MVC pattern.
I’m at the point where I want to create a reusable administration tools for diffrent object like entry, user, category. I will need specific sets of tools across diffrent controllers and views and multiple time.
I want to be able to call something like $this->load_admin_tools($name,$data) where $name will be the name of tool sets (which will be just a html forms) and $data necessary variables to process those forms (most likely $id of an object).
I want to do that so I could in one place add or remove some tools.
I was thinking to create a model_admin_tools where I will check for permission if the tools should be displayed and then load proper view (with forms to delete/edit/etc). I know that those should be a Controller (to check permissions and then decide if show something or not) but repeating same code over and over again it’s worst for me than putting those in to model. I could create private function in controller but then I would have to that function put in all my controllers.
Is there a better way to achieve this? I was thinking helpers, but again, helpers are not for “view”. Or maybe I’m just too officious?
Thanks in advance.
It’s not bad practice to use a helpers in a view. One helper that I use all the time in my views are the url helper.
If you want a set of tools that can be used across many controllers, just create a model that does whatever, and then in each of your controllers you can just load that model and then call the function. That’s the purpose of models is to be reused.
Also – you can return a view as a variable by setting a third parameter to true. See the very bottom of this page: http://codeigniter.com/user_guide/general/views.html
Example Controller method: