CodeIgniter - get error messages

送分小仙女□ 提交于 2019-12-08 07:18:59

问题


I created my own custom error page by changing the line in config.php

$route['404_override'] = 'main/_404';

So now it loads the correct page, by loading the _404 function in my controller

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>

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:


Well, actually, two things:

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.

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:

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.

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;
    }

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.



来源:https://stackoverflow.com/questions/7935004/codeigniter-get-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!