CodeIgniter Forums
Need to extend App_Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Need to extend App_Controller (/thread-59390.html)



Need to extend App_Controller - El Forum - 09-29-2013

[eluser]Unknown[/eluser]
Hello

I'm a beginner in CI.
Im working on a project which requires me to extend the core CI_Controller class
I did the following:
1- Enabled hooks through config file.
2- modified the subclass_prefix to empty string, as I dont want to use any prefix in my custom class.
3- created my class file 'application/core/Front_Controller.php' which contains
Code:
class Front_Controller extends CI_Controller {
// my code here
}
4- added the following to 'config/hooks.php' file:
Code:
$hook['post_controller'] = array(
                                'class'    => 'Front_Controller',
    'function'  => '__construct',
                                'filename' => 'Front_Controller.php',
                                'filepath' => 'core'
                                );

but I couldnt use my class in ci, I get the following error:
Quote:Fatal error: Class 'Front_Controller' not found in C:\AppServ\www\....\application\controllers\front\home.php on line 3

Could anyone please help and tell me what is wrong as I must do it this way?

Thanks


Need to extend App_Controller - El Forum - 09-29-2013

[eluser]Ckirk[/eluser]
Any reason why you aren't using the built in method of extending core classes?

if you create a file in application/core called "MY_Controller" with the following:
Code:
<?php
class MY_Controller extends CI_Controller{
    function __construct(){
     parent::__construct();
    }
    // Your code
}

using your example you'd use that controller like so:
Code:
class Front_Controller extends MY_Controller {
// my code here
}

see the manual for more info: http://ellislab.com/codeigniter/user-guide/general/core_classes.html




Need to extend App_Controller - El Forum - 09-30-2013

[eluser]AlexandrosG[/eluser]
And if you want to extend the CI_Controller in many ways you create the relevant classes:

Code:
class Snoopy_Controller extends CI_Controller {
...
}

class Goofy_Controller extends CI_Controller {
...
}

and you put them all in the same "MY_Controller.php" file.