In Codeigniter extends one controller to another controller

I am using codeigniter (3.1.5) and I have two controllers in my application / controllers / folder. with name controller A and controller B. I want to extend controller A in controller B so that I can use the methods of controller A. But it generates a class not found error.

Here's my sample code:

A_Controller.php:

defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {
  public function index()
  {
  }

  public function display(){
     echo 'base controller function called.';
  }
 }

      

B_Controller.php:

 defined('BASEPATH') OR exit('No direct script access allowed');
 class B_Controller extends A_Controller {

 }

      

I want to execute display () method of controller A in controller B. If I put controller A in application / core / folder and in application / config / config.php file I do

$ config ['subclass_prefix'] = 'A _';

then I can access the methods of controller A.

Please suggest. Thanks in advance.

+3


source to share


4 answers


Extending a controller on another controller is not good. When building MVC, and especially with CI, you have other options to achieve this goal.



  • Use a class MY_Controller

    inside application/core

    that extends CI_Controller

    . Later all (or some) of your controllers should expand MY_Controller

    . You can have many functions in MY_Controller, and you can call which function you want in your controller.
  • Use a library. Write your own library in application/libraries

    and load it in your controller wherever you want. A library is a class with functionality for your project.
  • Use an assistant. Write your own helper in application/helpers

    and load it into your controller. The helper should have a common goal for your application.

    This way your code will be more flexible and reusable for the future. Messing with two controllers seems bad to me. Be aware that with the standard CI routing system, you might be confused.

+3


source


Try the following code.



defined('BASEPATH') OR exit('No direct script access allowed');
     class B_Controller extends A_Controller {
        public function test()
        {
            $this->display();
        }
     }

      

+1


source


Use construct in controller

defined('BASEPATH') OR exit('No direct script access allowed');
 class B_Controller extends A_Controller {

    public function __construct() {
         parent::__construct();
    }
    public function test()
    {
        $this->display();
    }
 }

      

0


source


I found a solution using parent controller on child controller like this -

require_once(APPPATH."modules/frontend/controllers/Frontend.php");

then my function like this -


class Home extends Frontend {

    function __construct() {
        parent::__construct();
    }

    function index() {
        echo $this->test(); //from Frontend controller
    }

}

      

I hope this helps.

0


source







All Articles