You cannot call the same static method from the same method from the same class

I have this error controller in Codeigniter 2.1.0 application:

<?php class Error extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { set_status_header(404); $data->menuItems = Main::_menu(); $data->title = "404 error !"; $data->pageview = 'templates/404'; $this->load->view('templates/main', $data); } public function facebook() { set_status_header(404); $data->menuItems = Main::_menu(); $data->title = "Facebook error !"; $data->pageview = "templates/facebook_error"; $this->load->view('templates/main', $data); } } ?> 

Maincontroller _menu:

 <?php class Main extends CI_Controller { // ... a lot of methods here ... public static function _menu() { static $menuItems = array( //just a simple array ); } } ?> 

The facebook () method completely matches the index (), however the index works fine, facebook () gives this message:

 Fatal error: Class 'Main' not found in /var/www/MYApplicationName/application/controllers/error.php on line 22 

How is this possible? How can I reach Main::_menu() of facebook () method?

+6
source share
4 answers

Based on @TheShiftExchange's answer, I was able to track that the route setup caused this behavior. My config/routes.php looks like this:

 $route['404_override'] = 'error/index'; $route['(:any)'] = "main/$1"; 

So, when I made the request www.example.com/nonexistent-url , this request is served by the main controller, then CI noticed that there is no such method that it would execute error/index too, but the main controller was already loaded by then.

Another facebook method was redirected only from the existing main method, for example gallery , so it looks like I went to the URL www.example.com/error/facebook , so the main controller is not loaded because only error/facebook requested . If I call www.example.com/error/index , it works the same, because in this case the main controller does not load, only error .

(The bounty goes to @TheShiftExchange because his answer was the most accurate and provided me with the best information to track the problem. One of my redirects was never reached, which I call the error / index page.)

+1
source

Call functions from another controller interrupt MVC and put you in the HMVC area.

Please see this thread , as well as CodeIgniter Modular Extensions (HMVC) , if you really need to do this.

+1
source

Why don't you create a MY_Controller that extends CI_Controller where you put your reusable code?

+1
source

Actually - you are mistaken. Your "index" code also does not work.

I just put all your code in a clean version of CI 2.1.2, and the ALSO index function returns the same error.

So, maybe your computer has something cached, or you have different code elsewhere, which makes you think that it works. But that does not work.

In any case, the real question is why are you trying to call the controller from another controller? This is not a good MVC approach. Your function "_menu ()" should be located in the library where it belongs.

Or put the _menu () code in "MY_Controller" and both controllers expand on MY_Controller .

Or put the _menu () code in helper .

Basically, there are three best ways you could get close to this.

+1
source

Source: https://habr.com/ru/post/922921/


All Articles