Codeigniter extending extended MY_Controller

前端 未结 5 645
慢半拍i
慢半拍i 2020-12-09 21:07

I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.

My 3 classes:

// applicatio         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 21:51

    I have a custom controller class called MY_Controller it extends CI_Controller and it works fine. It is located at application/core and it has custom functions lo load views in my site.

    I use an abstract class My_app_controller that extends MY_Controller for my_app specific behabior, I want every controller in my_app to extend this abstract class. (I use diferent apps in the site, so some apps will extend My_app_controller and other apps will extend My_other_apps_controllers)

    But when I try to extend My_app_controller from any controller in my application, "Main_app_controller extends My_app_controller" generates a Class 'My_app_controller' not found exception.

    I found two solutions:

    1. use include_once in Main_app_controller.php file.
      include_once APPPATH.'controllers/path/to/My_app_controler.php';

    2. break the "one class per file" rule of code igniter and define my My_app_controller just in the same file MY_Controller is (under application/core).

    Manual says:

    Use separate files for each class, unless the classes are closely related

    Well... they are.

    Anyway, I prefered to use the include_once solution as I think it is better to have one file per class and My_app_controller is located under application/controllers/my_app folder. (so application/controllers/other_apps will exist)

提交回复
热议问题