codeigniter: extending common controller

后端 未结 3 2038
长情又很酷
长情又很酷 2021-01-24 21:31

I\'ve read all the post I found regarding this issue but nothing works. I\'m using Codeigniter 2.02 in a LAMP with Apache2.2 and PHP5.3.2

I\'m trying to create a common

相关标签:
3条回答
  • 2021-01-24 21:36

    This bit is correct

    public function __contstruct() instead of public function Parent_controller()

    But what you're looking for is the MY_ prefix. So if you create the controller in the /application/libraries/ folder and call the file MY_Controller.php and the class MY_Controller it'll work.

    You can also change the MY_ prefix to whatever you'd like in the config.php file. Look for:

    /*
    |--------------------------------------------------------------------------
    | Class Extension Prefix
    |--------------------------------------------------------------------------
    |
    | This item allows you to set the filename/classname prefix when extending
    | native libraries.  For more information please see the user guide:
    |
    | http://codeigniter.com/user_guide/general/core_classes.html
    | http://codeigniter.com/user_guide/general/creating_libraries.html
    |
    */
    $config['subclass_prefix'] = 'MY_';
    

    For further reading and a more depth explanation see http://codeigniter.com/user_guide/general/core_classes.html

    0 讨论(0)
  • 2021-01-24 21:39

    Also note it doesn't load a multitude of files. It simply looks for 1 controller called MY_Controller.php.

    If you are thinking it will load MY_Test_Controller.php and MY_Web_Controller.php, it won't.

    If you can include multiple controllers in that one file, or include other files from that file.

    You could build around this of course, but a good bit of extra information to know.

    0 讨论(0)
  • 2021-01-24 21:52

    Take a look at this post from Phil Sturgeon:

    http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

    The key is using the native autoload as explained in his post:

    /*
    | -------------------------------------------------------------------
    |  Native Auto-load
    | -------------------------------------------------------------------
    | 
    | Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
    | for base controllers and some third-party libraries.
    |
    */
    function __autoload($class)
    {
        if(strpos($class, 'CI_') !== 0)
        {
            @include_once( APPPATH . 'core/'. $class . EXT );
        }
    }
    

    NOTE

    As a note, you'll want to put all of your "base" controllers in the core folder for CI2+

    0 讨论(0)
提交回复
热议问题