![]() |
Extending Controller class - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Extending Controller class (/showthread.php?tid=13725) |
Extending Controller class - El Forum - 12-03-2008 [eluser]reaktivo[/eluser] I have searched the forum, and have not found a real solution, is there a way to extend system/libraries/Controller.php like other classes that use MY_Name_of_class without having to extend MY_Controller on the Controllers placed on application/controllers/whatever ?, can't I add functionality to a Controller without modifying every Controller I have coded already ? Extending Controller class - El Forum - 12-03-2008 [eluser]xwero[/eluser] Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers. The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class. Code: // MY_Controller.php Extending Controller class - El Forum - 12-03-2008 [eluser]wiredesignz[/eluser] You may create application/libraries/Controller.php yourself. Copy the original contents of system/libraries/Controller.php and add your own extensions as needed. Every controller in your application will inherit your additions. Extending Controller class - El Forum - 12-03-2008 [eluser]reaktivo[/eluser] [quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers. The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class.[/quote] Thanks xwero. Extending Controller class - El Forum - 12-03-2008 [eluser]Colin Williams[/eluser] wired's solution is usually the best place to start, in my opinion. Nothing wrong with "grandfathering" the Controller class like xwero points out, either. Choose your medicine.. Extending Controller class - El Forum - 12-03-2008 [eluser]Rey Philip Regis[/eluser] [quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers. The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class. Code: // MY_Controller.php Didn't know this yet..But at least I know now..What's the use of creating a new controller when you can extend it in you application? What do you thing reactivo trying to do? Just curious.... Extending Controller class - El Forum - 12-03-2008 [eluser]Colin Williams[/eluser] Anything "global" Rey. Like, load up menus from your menu system. Load up sidebar content, header content, footer content, etc. This leaves your individual controllers free to worry only about their specific task, and not each final piece of the layout. Extending Controller class - El Forum - 12-03-2008 [eluser]Rey Philip Regis[/eluser] [quote author="Colin Williams" date="1228362433"]Anything "global" Rey. Like, load up menus from your menu system. Load up sidebar content, header content, footer content, etc. This leaves your individual controllers free to worry only about their specific task, and not each final piece of the layout.[/quote] Hi Colin thnks for the reply but im having trouble grasping what youve posted. Can you explain more on this. Actually I dont have a use for this as of now, but maybe I maybe using this technique in the future. Good day. Extending Controller class - El Forum - 12-03-2008 [eluser]reaktivo[/eluser] [quote author="Rey Philip Regis" date="1228362098"][quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers. The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class. Code: // MY_Controller.php Didn't know this yet..But at least I know now..What's the use of creating a new controller when you can extend it in you application? What do you thing reactivo trying to do? Just curious....[/quote] I really don't know if this is the best way of doing this task but... I was looking for a way to remove form submission validation on Controllers, and allow all Controllers to validate different forms, for example a user auth form submitted and displayed on different view's loaded on different Controllers. You would not want to check for user authentication on each controller. So I added some code to the Controller constructor to load and call a model method, here it is: Code: if($service = $this->input->post('service')) { This removes the need for my Form to be tied to a Controller, also, it allows to make ajax request without the complete page being returned. And since in CI 1.7 I can have form_validation on the model itself, I think is nice. I you feel you have a better way of doing this, please let me know! Thanks. |