php 在 CodeIgniter 中显示 404 错误的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6758275/
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
Display a view for 404 error in CodeIgniter
提问by Roman
The CodeIgniter has a very simple default error 404 message:
CodeIgniter 有一个非常简单的默认错误 404 消息:
404 Page Not Found
The page you requested was not found.
Instead of using this error message on totally blank page, I want to wrap this message in between my header and footer view, so that the error message have similar look to the other pages.
我不想在完全空白的页面上使用此错误消息,而是想将此消息包装在页眉和页脚视图之间,以便错误消息与其他页面具有相似的外观。
For that purpose, I have created an error view? For example:
为此,我创建了一个错误视图?例如:
my404_view.php
my404_view.php
<? $this->load->view('header'); ?>
404 Page Not Found
The page you requested was not found.
<? $this->load->view('footer'); ?>
Now, How can I use this my404_view.php
as a default view to display 404 messages instead of using the CodeIgniter default error message.
现在,我如何使用它my404_view.php
作为默认视图来显示 404 消息,而不是使用 CodeIgniter 默认错误消息。
回答by Alfonso Rubalcava
You should change your routes.php. For example:
你应该改变你的routes.php。例如:
in application/config/routes.php
在 application/config/routes.php
$route['404_override'] = 'welcome/_404';
in application/controllers/welcome.php
在应用程序/控制器/welcome.php
function _404(){
$this->load->view("my404_view");
}
And this should be sufficient in the current version of CI.
这在当前版本的 CI 中应该足够了。
回答by DavidHyogo
Including headers and footers in the default error 404 page seems to be a common problem for CodeIgniter users. I would like to add this link: Simon Emms's comments at http://www.simonemms.com/2011/05/06/codeigniters-404-override-problem/because he describes the problem so clearly. I have tried the suggestions at http://maestric.com/doc/php/codeigniter_404and played around with Simon Emms's ideas, and others, but just can't implement them. I'm a bit of a novice at PHP and CodeIgniter, so that might be because of ignorance. That said, it is difficult to ensure that you put the suggested subclasses in the right places and configure, for instance, routes.php correctly. After 3 days of trying the various rather complicated ideas, it occurred to me that I could just use an include statement in the default error 404 page. The only difficulty was figuring out the path to pass to the include statement. In /system/core/Exceptions.php line 146, I found the CodeIgniter developers use APPPATH. You then just have to append the path to the header and footer pages you want to include. My new default error 404 page now looks like this:
在默认的 404 错误页面中包含页眉和页脚似乎是 CodeIgniter 用户的常见问题。我想添加此链接:Simon Emms 在http://www.simonemms.com/2011/05/06/codeigniters-404-override-problem/ 上的评论,因为他非常清楚地描述了问题。我已经尝试了http://maestric.com/doc/php/codeigniter_404 上的建议并尝试了 Simon Emms 和其他人的想法,但就是无法实施。我是 PHP 和 CodeIgniter 的新手,所以这可能是因为无知。也就是说,很难确保您将建议的子类放在正确的位置并正确配置,例如,routes.php。在尝试各种相当复杂的想法 3 天后,我突然想到我可以在默认错误 404 页面中使用 include 语句。唯一的困难是找出传递给 include 语句的路径。在 /system/core/Exceptions.php 第 146 行,我发现 CodeIgniter 开发人员使用 APPPATH。然后,您只需将路径附加到要包含的页眉和页脚页面。我的新默认错误 404 页面现在如下所示:
<?php
include APPPATH.'/views/templates/header.php';
?>
<div id="container">
<h1><?php echo $heading; ?></h1>
<?php echo $message; ?>
</div>
<?php
include APPPATH.'/views/templates/footer.php';
?>
This seems a much easier solution to me than trying to change the core classes in CodeIgniter.
这对我来说似乎比尝试更改 CodeIgniter 中的核心类要容易得多。
回答by Sukumar
There is quite a bit of information on this.
有很多关于这方面的信息。
http://maestric.com/doc/php/codeigniter_404
http://maestric.com/doc/php/codeigniter_404
http://www.nickyeoman.com/blog/apache/90-htaccess-404-page
http://www.nickyeoman.com/blog/apache/90-htaccess-404-page
http://hasitha.posterous.com/customising-error-pages-on-codeigniter (archived)
http://hasitha.posterous.com/customising-error-pages-on-codeigniter(存档)