CodeIgniter - 获取错误消息 [英] CodeIgniter - get error messages
问题描述
我通过更改config.php中的行创建了自己的自定义错误页面
I created my own custom error page by changing the line in config.php
$route['404_override'] = 'main/_404';
现在它加载正确的页面,通过在我的控制器中加载_404函数
So now it loads the correct page, by loading the _404 function in my controller
问题是,我仍然希望能够获得$ heading& $ message错误变量显示在默认页面,显示为:
The issue is, I still want to be able to get a hold of the $heading & $message error variables displayed in the default page, which show up as:
<div id="container">
<h1><?php echo $heading; ?></h1>
<?php echo $message; ?>
</div>
这是我的_404函数,如果任何人都可以给我一些建议如何添加这些变量我会非常感谢
Here's my _404 function if anyone can give me some advice how to add those variables I would greatly appreciate it
public function _404() {
$data['query'] = array('title' => 'Page not found.', 'keywords' => '', 'description' => 'Page not found', 'page' => 'error');
$this->load->view('parts/head',$data);
$this->load->view('parts/_404');// <- would go here
$this->load->view('parts/footer');
}
推荐答案
:
1)在重新路由的404网页中,您应该可以定期传递任何您想要的变数,让您只需 $ this - > load-> view('parts / _404',$ data);
,并有可用的变量。
1) In your re-routed 404 page, you should be able to pass regularly any variable you want, so you can simply $this->load->view('parts/_404',$data);
and have there available your variables.
如果你正在谈论默认404页面,记住它不能在某些情况下被覆盖,也就是当调用 show_404()
核心函数时:
2) if you're talking about the default 404 page, keep in mind that it can't be overridden under certain circustamces, that is when the show_404()
core function is called:
它不会影响show_404()函数,这将继续
加载默认的error_404.php文件
application / errors / error_404.php。
It won't affect to the show_404() function, which will continue loading the default error_404.php file at application/errors/error_404.php.
此函数属于异常处理程序类。实际上,在90行你有
This function belongs to the Exception handler class. There, in fact, at line 90 you have
function show_404($page = '', $log_error = TRUE)
{
$heading = "404 Page Not Found";
$message = "The page you requested was not found.";
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', '404 Page Not Found --> '.$page);
}
echo $this->show_error($heading, $message, 'error_404', 404);
exit;
}
,然后调用 show_error code>方法设置标头错误代码(第四个参数)并添加指定的视图(第三个参数) 。
which in turn calls the show_error()
method wich sets the header error code (4th argument) and adds the specified view (3rd argument) to the view buffer.
正如你所看到的,消息在这里被硬编码在方法中。如果您想要进行总体自定义,您可以覆盖此方法(例如,调用同一类中的另一个函数)或者简单地硬编码其他消息。
As you can see messages are here hardcoded inside the method. If you want a total customization you can either override this method (make it, for example, call another function within the same class) or simply hardcode other message there instead.
这篇关于CodeIgniter - 获取错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!