php 找不到代码点火器 MY_Controller

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13483677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:33:23  来源:igfitidea点击:

codeigniter MY_Controller not found

phpcodeignitercontrollersextending-classes

提问by Yahya KACEM

i'm using the Codeigniter.2.1.3 for a website, so i need to extend the CI_Controller so i can add a method to be executed with all controllers so i did what's in the user_guide:

我正在为网站使用 Codeigniter.2.1.3,所以我需要扩展 CI_Controller 以便我可以添加一个与所有控制器一起执行的方法,所以我做了 user_guide 中的内容:

creating a file named MY_Controller.php in the application/core folder the creating in it MY_Controller Class that extends the CI_Controller, the changing my regular controller to extend the MY_controller like this: MY_controller.php:

在 application/core 文件夹中创建一个名为 MY_Controller.php 的文件,在其中创建扩展 CI_Controller 的 MY_Controller 类,更改我的常规控制器以扩展 MY_controller,如下所示: MY_controller.php:

class MY_Controller extends CI_Controller{
    protected $page;
    # Constructor
    function __construct (){
        parent::__construct();
        #code shared with all controllers
    }
    public function get_page(){
        #code to get_the right page here
    }
}

regular controller named Regular.php:

名为 Regular.php 的常规控制器:

class Regular extends MY_Controller{
     public function __construct(){
         parent::__construct();
     }
     public function index(){
          $this->get_page();
     }
}

but the following error keep appearing:

但以下错误不断出现:

Fatal error: Class ‘MY_Controller' not found in /var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php on line 2

致命错误:在第 2 行的 /var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php 中找不到“MY_Controller”类

回答by Maxime Morin

You would need to include your MY_Controllerclass or auto-load it. I suggest you auto-load it by adding the following to your application/config/config.phpfile.

您需要包含您的MY_Controller类或自动加载它。我建议您通过将以下内容添加到application/config/config.php文件中来自动加载它。

function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
    }
} 

回答by Dibyendu Mitra Roy

Make sure the filename is perfectly cased. Linux server is case-sensitive. So if the class name is My_Controller then the name of the file should be My_Controller.php

确保文件名大小写完美。Linux 服务器区分大小写。所以如果类名是 My_Controller 那么文件名应该是 My_Controller.php

回答by ronihasan

$config['subclass_prefix'] = "MY_"

$config['subclass_prefix'] = "MY_"

check that in config.phpand of course you should use it in Controller Name Like MY_Controller.phpand Named "class MY_Controller...."

检查,config.php当然你应该在 Controller Name LikeMY_Controller.php和 Named "class MY_Controller ...." 中使用它

回答by antelove

in config/config.php

config/config.php

/* load class in core folder */
function my_load($class) {        

    if (strpos($class, 'CI_') !== 0) {            
        if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {                
            require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');                
        }
    }

}

spl_autoload_register('my_load');

回答by ScottyB

Late with this answer, but I got the

这个答案迟到了,但我得到了

"Fatal error: Class ‘MY_Controller' not found" error

“致命错误:未找到‘MY_Controller’类”错误

when I had a controller (.php) file by the same name at the web root instead of the application/controllers directory.

当我.php在 web 根目录而不是 application/controllers 目录中有一个同名的控制器 ( ) 文件时。

Don't know how it got there, actually, but the problem went away when I deleted it.

不知道它是如何到达那里的,实际上,但是当我删除它时问题就消失了。