Extending The Controller Class in CodeIgniter

前端 未结 5 1523
一向
一向 2020-11-27 20:23

I have class MY_Controller extends CI_Controller and common logic for big profile section, so I\'va tried to create class Profile extends MY_Controller

相关标签:
5条回答
  • 2020-11-27 20:34

    After some struggle with version 3 and this issue I decided this was not a bad solution...

    require_once BASEPATH.'core/Controller.php';
    require_once APPPATH.'core/MYCI_Controller.php';
    

    to add this second line where the first exists in the system/core/CodeIgniter.php

    [If it's not too late, I recommend strongly against php and/or CodeIgniter.]

    0 讨论(0)
  • 2020-11-27 20:36

    It is possible with Codeigniter 3. Just including the parent file is enough.

    require_once(APPPATH."controllers/MyParentController.php");
    class MyChildController extends MyParentController {
    ...
    
    0 讨论(0)
  • 2020-11-27 20:40

    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();
        }
    ...
    }
    
    0 讨论(0)
  • 2020-11-27 20:43

    I found this page on Google because I had the same problem. I didn't like the answers listed here so I created my own solution.

    1) Place your parent class in the core folder.

    2) Place an include statement at the beginning of all classes that include the parent class.

    So a typical controller might look like this:

    <?php
    
    require_once APPPATH . 'core/Your_Base_Class.php';
    // must use require_once instead of include or you will get an error when loading 404 pages
    
    class NormalController extends Your_Base_Class
    {
        public function __construct()
        {
            parent::__construct();
    
            // authentication/permissions code, or whatever you want to put here
        }
    
        // your methods go here
    }
    

    The reason I like this solution is, the whole point of creating a parent class is to cut down on code repetition. So I don't like the other answer that suggested copy/pasting the parent class into all of your controller classes.

    0 讨论(0)
  • 2020-11-27 20:52

    All classes you are extending should live in application/CORE directory so in your case both My_Controller and Profile should live there. All "end point" controllers will live in application/controllers folder

    UPDATE

    I stand corrected. Extended classes should live in the same file. @Rooneyl's answer shows how to implement

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