简体   繁体   中英

Codeigniter MY_Controller: is it only possible to extend core once?

I have successfully extended the core using the MY_Controller as described in CI's documentation.

This way I can put some repetitive functions (ie, auth check) in the constructor of MY_Controller so they always run before methods from my other controllers.

My issue now is that some parts of my webapp are open (ie, don't require login) and others require login.

Therefore, I cannot extend ALL my controllers from MY_Controller (which contains an auth check function).

I wondered if it would be possible to extend the core so as to have, say, LOG_Controller and NOLOG_Controller.

Then, controllers that require login would extend from LOG_Controller --- and controllers that do not require login would extend from NOLOG_Controller.

Is this posssible? (or is it bad form?)

It seems config/config.php only allows for one core extension prefix so I'm not sure it's possible.

Let me know what you think or if there's a better way of doing this. Thanks.

I use the following trick. Define all your base controllers in My_Controller.php:

<?php

class My_Controller extends CI_Controller{

}

class LOG_Controller extends My_Controller{

} 

class NOLOG_Controller extends My_Controller{

}
?>

This makes all defined controllers available later in your controllers. Just avoid making the base controllers too fat.

Also you can check this example: https://github.com/aquilax/novigeroi2/blob/master/application/core/AQX_Controller.php

Since you haven't posted any source code, let's imagine your MY_Controller looks similar to the following example:

File path: application/core/MY_Controller.php

class MY_Controller extends CI_Controller
{
    function __construct() {
        parent::__construct();

        if ( !$this->ion_auth->logged_in() ) {
            redirect('auth/login');
        }
    }
}

Your "protected" controllers extend this one and inherit it's methods, as follows:

File path: application/controllers/Secure.php

class Secure extends MY_Controller
{
    function __construct() {
        parent::__construct();
    }
}

You want to allow both some protected and some unprotected controllers in your application. In order for you to keep those controllers protected, they must extend MY_Controller and inherit it's __construct() method. For any controller that doesn't need to inherit that __construct() method, you simply extend the CI_Controller directly, as follows:

File path: application/controllers/Insecure.php

class Insecure extends CI_Controller
{
    function __construct() {
        parent::__construct();
    }
}

There you have it. Controllers which inherit directly from ( extend ) the CI_Controller will be "insecure".

One Consideration

The main consideration here is that you may have additional methods in your controller that you want to pass to all your controllers - in which case this may not be the best solution for you. If that is the case, you can always move those methods to a helper instead.

Another easy solution is to simply require the additional classes/controllers you need at the bottom of your application/controller/my_controller.php file

My custom controller looks like this, didn't require any additional configuration settings:

class My_Controller extends CI_Controller {

    // do something clever here

}

// do something even more clever here ...
require_once("application/core/tool_controller.php");

Seems pretty dry to me and then allows you to customize your application by setting up a templates / layouts controller directory somewhere. I'm just finishing up an older CI project so I'm taking it further but I'm sure you could also play around with some config variables elsewhere to make it even DRY-er

I think it is not possible. There are ways of doing what you need extending the same controller. Why not put all your open pages in a folder (like Public) and check in MY_Controller if the request is directed to that folder. If not, requires auth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM