Domanda

I need a hand here guys, I am confuse on how I will be placing my files for my code to work out. I had 2 controllers named

ex: 
controller1.php
controller2.php

which I want all my functions in controller1.php accessible in controller2.php so I write

class Controller2 extends Controller1 { 

}

which I had applied what I had understand here Calling a Controller function in another Controller in CodeIgniter

But I am getting an error

Fatal error: Class 'Controller1' not found in C:\xampp\htdocs\bla\application\controllers\Controller2.php on line 3

Then I had search for any possible solution then i had found out that i had to place Controller1.php in the core folder but I am getting the same error..MY_Controller class not found

I am using the latest version of codeigniter framework.

What I had missed? How?

È stato utile?

Soluzione

Rename the file and class to "MY_Controller" if you placed it into the "/core" folder. The prefix is important (which you can set in your config, by the way). Also, make sure, that in MY_Controller, you extend the CI_Controller

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

You can then access this core-Controller with your other Controllers like this:

class Controller1 extends MY_Controller
{
    public function __construct()
    {
        parent::_construct();
    }
}

Altri suggerimenti

Controller1 will be auto-loaded and be able to use for extending if you place it inside application/core/MY_Controller.php.

Otherwise, you have to use require:

require APPPATH . 'controllers/Controller1.php';
class Controller2 extends Controller1 { }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top