php codeigniter 3.0 自定义 404 未找到错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34478283/
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
codeigniter 3.0 custom 404 not found error page
提问by Ramin
My problem is about custom error pages after upgrading to Codeigniter 3.0.
我的问题是关于升级到 Codeigniter 3.0 后的自定义错误页面。
I used to have a Errors
controller which handled 404 error page of the website - on this controller I have customized some parts of page, such as if user is logged in, it was showing username on the navigation bar and etc. (yes, on 404 error page).
我曾经有一个Errors
控制器来处理网站的 404 错误页面 - 在这个控制器上我自定义了页面的某些部分,例如如果用户登录,它在导航栏上显示用户名等等(是的,在 404错误页面)。
After upgrading to CI 3.0, I have noticed that error pages are handled in different folder, I started to migrate to this way; but the problem is I can't load any variables to this page, such as session variablesor can't even use any CI's functions on these pages.
升级到CI 3.0后,发现错误页面在不同的文件夹中处理,我开始迁移到这种方式;但问题是我无法将任何变量加载到此页面,例如会话变量,甚至无法在这些页面上使用任何 CI 函数。
I think this pages are supposed to be only HTML pages, but is there any way to load variables to these error pages?
我认为这个页面应该只是 HTML 页面,但是有没有办法将变量加载到这些错误页面?
回答by Gopakumar Gopalan
You need to set in application/config/routes.php
the following route
您需要在application/config/routes.php
以下路线中设置
$route['404_override'] = 'my404';
Then you need to create a new controller in your controllers directory (application/controllers/)
然后你需要在你的控制器目录中创建一个新的控制器 (application/controllers/)
<?php
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404');//loading in custom error view
}
}
And create an err404
view. Thats all!
并创建一个err404
视图。就这样!
Refer :Reserved Routes
参考:保留路线
回答by midhun prakash
It's not working in codeigniter 3, I overwrite the default view (application/views/errors/html/error_404.php
) and added my custom view, now show_404()
is working fine.
它在 codeigniter 3 中不起作用,我覆盖了默认视图 ( application/views/errors/html/error_404.php
) 并添加了我的自定义视图,现在 show_404()
工作正常。
回答by Javier Gordo
that is working good, but if redirect to an error, for example 404 with function show_404, your controller wont load your view, for a real 404 in the show_404 method, you have to go to the core of codeigniter and touch it.
这很好用,但是如果重定向到错误,例如带有函数 show_404 的 404,您的控制器将不会加载您的视图,对于 show_404 方法中的真正 404,您必须转到 codeigniter 的核心并触摸它。
\system\core\Common.php
\system\core\Common.php
this is an example of code:
这是一个代码示例:
function show_404(){
$ci = get_instance();
$ci->output->set_status_header(404);
$ci->load->view('errors/error404_view');
echo $ci->output->get_output();
exit(4);
}
回答by Adam O'Dwyer
In Codeigniter 3, instead of calling show_404()
, I just redirected to the custom error controller function.
在 Codeigniter 3 中show_404()
,我没有调用,而是重定向到自定义错误控制器函数。
e.g. for the above scenario
例如对于上述场景
if (404 condition...) {
redirect("my404");
}