php CodeIgniter:找不到类“CI_Controller”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6758681/
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-26 01:10:19  来源:igfitidea点击:

CodeIgniter: Class 'CI_Controller' not found

codeigniterphp

提问by Roman

I've extended CodeIgniter controller by adding MY_Controller.php in Application/Core folder. It works fine, but Now when I add following code on error_404.phppage in Application/errors, I get error.

我通过在 Application/Core 文件夹中添加 MY_Controller.php 扩展了 CodeIgniter 控制器。它工作正常,但是现在当我在error_404.php页面中添加以下代码时Application/errors,出现错误。

Code causing problem:

导致问题的代码:

<?php $ci =& get_instance();?>
<?php $this->ci->load->view('header')?>

Error:

错误:

Fatal error: Class 'CI_Controller' not found in path\to\system\core\CodeIgniter.php on line 231

The line 231 of the system\core\CodeIgniter.phpis:

的第 231 行system\core\CodeIgniter.php是:

function &get_instance()
    {
        return CI_Controller::get_instance(); 
    }

How can I fix this so that I can load view in the error_404.php without changing anything in system files.

我该如何解决这个问题,以便我可以在 error_404.php 中加载视图而不更改系统文件中的任何内容。

PS. I'm using latest version.

附注。我正在使用最新版本。

Thanks.

谢谢。

采纳答案by Wesley Murch

I don't think CI_Controller is loaded yet. The Exception class is handling the 404 page output using an include.

我认为 CI_Controller 尚未加载。Exception 类使用include.

In the old days, I used to use straight includes to piece together a template, or do a file_get_contents()or cURL request to my 404 page URL, but they finally did something about it. In 2.0 you can define a custom 404 page in routes.php:

在过去,我曾经使用直includes 来拼凑模板,或者file_get_contents()对我的 404 页面 URL执行或 cURL 请求,但他们最终对此有所作为。在 2.0 中,您可以在以下位置定义自定义 404 页面routes.php

$route['404_override'] = 'controller/method/parameter';

It's still not a perfect solution, but the easiest way now is just to use the route.

这仍然不是一个完美的解决方案,但现在最简单的方法就是使用路由。

Note that base_url()."controller/method/parameter"must be a valid url, and you should make sure to set a 404 header in the controller that outputs the page too (it's not done automatically for some reason).

请注意,base_url()."controller/method/parameter"必须是一个有效的 url,并且您应该确保在输出页面的控制器中设置一个 404 标头(由于某种原因它不会自动完成)。

回答by AndPy

I had this problem when following the CodeIgniter tutorial here: http://codeigniter.com/user_guide/tutorial/static_pages.html

我在遵循 CodeIgniter 教程时遇到了这个问题:http: //codeigniter.com/user_guide/tutorial/static_pages.html

The problem was I tried to access the url:

问题是我试图访问网址:

localhost/CodeIgniter_2.1.1/application/controllers/pages.php

本地主机/CodeIgniter_2.1.1/application/controllers/pages.php

instead of addressing the url:

而不是寻址网址:

localhost/CodeIgniter_2.1.1/index.php/pages/view

本地主机/CodeIgniter_2.1.1/index.php/pages/view

I know it has been around 18 months since you asked that question but maybe it can help someone else :)

我知道您问这个问题已经大约 18 个月了,但也许它可以帮助其他人:)

回答by tomhre

or you could try this, perhaps bit dirty but i think it will work for me

或者你可以试试这个,也许有点脏,但我认为它对我有用

update your exception to

将您的例外更新为

function show_404($page = '', $log_error = TRUE)  {
   if ($log_error)   {
     log_message('error', '404 Page Not Found --> '.$page); 
   } 
   header('Location: http://www.########.###/error/error_404');  
   die();
}

then just create controller to handle the redirect, perhaps with different errors you can use the same controller

然后只需创建控制器来处理重定向,也许有不同的错误,您可以使用相同的控制器

that way you will get your dynamic header and footer

这样你就会得到你的动态页眉和页脚

回答by simnom

Try:

尝试:

<?php $ci =& get_instance();?>
<?php $ci->load->view('header')?>

Once you assigned the instance of CI to $ci you can just use $ci->load->view('header) throughout your file.

将 CI 实例分配给 $ci 后,您只需在整个文件中使用 $ci->load->view('header) 即可。