Extending The Controller Class in CodeIgniter
I take it you have put your MY_Controller in /application/core, and set the prefix in the config.
I would be careful about using index as a class name though. As a function/method in Codeigniter it has a dedicated behaviour.
If you then want to extend that controller you need to put the classes in the same file.
E.g. In /application core
/* start of php file */
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
class another_controller extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
/* end of php file */
In /application/controllers
class foo extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
or
class bar extends another_controller {
public function __construct() {
parent::__construct();
}
...
}
How do I extend the code igniter controller class?
DD_Controller.php should be in /system/application/libraries/
If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.
In system/libraries/Controller.php below the Controller class:
class Mega_Controller extends Controller {
function Mega_Controller()
{
parent::Controller();
// anything you want to do in every controller, ye shall perform here.
}
}
Then you'll be able to do this in your app controllers:
class Home extends Mega_Controller {
....
Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.
Codeigniter extending controller, controller not found
I had the same problem, but if I created all controllers in the MY_Controller.php file all worked well.
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// do some stuff
}
}
class MY_Auth_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
// check if logged_in
}
}
Related Topics
How to Use Mamp's Version of PHP Instead of the Default on Osx
Stop Caching for PHP 5.5.3 in Mamp
Best Practices: Working with Long, Multiline Strings in PHP
File_Get_Contents Behind a Proxy
Split String by Delimiter, But Not If It Is Escaped
Back to Previous Page with Header( "Location: " ); in PHP
Send Email with PHPmailer - Embed Image in Body
Laravel Recursive Relationships
You Must Enable the Openssl Extension to Download Files via Https
How to Get Customer Details from an Order in Woocommerce
Passing Data to Buildform() in Symfony 2.8, 3.0 and Above
PHP Regex - Valid Float Number
Using PHP Variables Inside HTML Tags
Preventing Caching of CSS Files
Laravel 5.3 Auth Check in Constructor Returning False