How to extend Ci_controller to more than one core?

Asked

Viewed 664 times

1

In Codeigniter you can create a file in the application/core folder MY_Controller.php and the controller extending this file, a basic example would be:

<?php
class MY_Controller extends CI_Controller {

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

        $this->load->helper('url');
    }

}

What I need is to create other files in this core extending the Ci_controller itself as for example a MY_Admincontroller.php and he would have another model for My_controller, for example:

<?php
class MY_Admincontroller extends CI_Controller {

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

        $this->load->helper('admin');
    }

}

Only that doing so gives me a mistake that did not find the My_admincontroller.

Fatal error: Class 'My_admincontroller' not found in ...

I know it is possible to create this My_admincontroller right below My_controller, in the same file and so on, but with this the code will be much more polluted. Does anyone know if it’s possible to do what I want?

A way to do it, well summarized, but as I said, the code itself gets very polluted and with that difficult maintenance

<?php
class MY_Controller extends CI_Controller {}

class MY_Admincontroller extends MY_Controller {}

class MY_Othercontroller extends MY_Admincontroller {}
  • glue the error that is giving...

  • In your project (root) you have two other subprojects that use/call the same framework but the second of the error? is this situation? if it is see if any of these reply help

  • @rray then, not that not, actually, in the same project I want to be able to create a My_controller that extends from Ci_controller, also to be able to create My_admincontroller that also extends from Ci_controller and then with this in my controllers folder I can have several controllers, some extending from My_controller and others from My_admincontroller

1 answer

1


You can do this simply by including the base controller file as follows, when using it:

include_once(APPPATH.'core/Nome_Controller.php');

For example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once(APPPATH.'core/Admin_Controller.php');

class MySite extends Admin_Controller {

    public function index()
    {
        $this->load->view('my_site');
    }

}

Another way, even more usual, would be using autoload, where you do not need to include in all your controllers of the base controller file. As follows:

At the end of the file application/config/config.php or anywhere in it includes this autoload function load_my_controllers:

inserir a descrição da imagem aqui

function load_my_controllers($class) {

  $path = APPPATH . 'core/' . $class . '.php';

  if (strpos($class, 'CI_') !== 0 && is_readable($path)) {
    require_once($path);
  }

}

spl_autoload_register('load_my_controllers');

Once done, you can now include more than one base controller in the folder core of its application.

inserir a descrição da imagem aqui

  • 1

    I wanted to say that I tested here the way that Yure Pereira spoke, and it worked perfectly. I’ve been trying to solve this problem for some time, they can test, it works! OBS.: I’m using CI version 3.1.6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.