Extending the controller class - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Extending the controller class (/showthread.php?tid=35066) |
Extending the controller class - El Forum - 10-18-2010 [eluser]richzilla[/eluser] Hi All, When extending the controller class, does it have to contain the name of the class that it is extending? for example MY_Controller contains the name of the controller class that it is extending. The reason i ask is that i am attempting to extend the controller class twice, once for the admin part of my site and once for the normal 'site'. Ive named the new classes MY_Site and MY_Admin and placed them both in the libraries folder, however this does not seem to work, as nothing is generated when i request pages that implement these classes? any help would be appreciated. Extending the controller class - El Forum - 10-18-2010 [eluser]n0xie[/eluser] Read this Extending the controller class - El Forum - 10-19-2010 [eluser]richzilla[/eluser] thanks for the help, ive tried the suggestions mentioned there, but still no luck. so far i have: a MY_Controller: Code: class MY_Controller extends Controller { A Site_Controller: Code: class Site_Controller extends MY_Controller{ and an Admin_Controller: Code: class Admin_Controller extends MY_Controller { but still no luck, it should be noted that if simply extend my usual controllers directly from the Controller class, they work without a hitch, so my code is ok. Also on a slightly different note, what is the purpose of this : Code: function __autoload($class) Any help would be appreciated. Extending the controller class - El Forum - 10-19-2010 [eluser]kaejiavo[/eluser] [quote author="ricardino" date="1287523660"]thanks for the help, ive tried the suggestions mentioned there, but still no luck. so far i have: a MY_Controller: Code: class MY_Controller extends Controller { A Site_Controller: Code: class Site_Controller extends MY_Controller{ and an Admin_Controller: Code: class Admin_Controller extends MY_Controller { but still no luck, it should be noted that if simply extend my usual controllers directly from the Controller class, they work without a hitch, so my code is ok. Also on a slightly different note, what is the purpose of this : Code: function __autoload($class) Any help would be appreciated.[/quote] Hi, you have to use only one file: MY_Controller.php as this will be autoloaded by CI. Then you put all your three classes in this file, like: Code: class MY_Controller extends Controller { Then you can extend your Site_Controller or Admin_Controller as you like. Marco Extending the controller class - El Forum - 10-19-2010 [eluser]richzilla[/eluser] i put everything into the same file - didnt seem to work, i get absolutely no output to the screen when i do that. Just to clear up any confusion im using CI 1.7.2 if thats an issue? Extending the controller class - El Forum - 10-19-2010 [eluser]InsiteFX[/eluser] MY_Controller need to go into application/libraries If running CodeIgniter 2.0 MY_Controller needs to go into application/core Code: // PHP 5 if you do not have an index function and load a view you will get a blank page. InsiteFX Extending the controller class - El Forum - 10-19-2010 [eluser]cahva[/eluser] The purpose of __autoload is that when using PHP5, it will automatically load your classes from the libraries folder if they are the same name. When using this __autoload, you dont actually need MY_Controller at all. Put the autoload to the config.php as suggested. Create Admin_Controller.php to libraries folder. Code: class Admin_Controller extends Controller { Create Site_Controller.php to libraries folder. Code: class Site_Controller extends Controller { Then in controllers you extend from one of those. Normal pages would use Site_Controller and pages that needs authenticated user extends from Admin_Controller. controllers/home.php Code: class Home extends Site_Controller { controllers/backend.php Code: class Backend extends Admin_Controller { Try it. Should work(unless I had some typos ). BTW, base controllers dont use index() methods. And always use __construct as constructor(you had some mixed constructors with PHP4/PHP5 style in your code). |