Marius Miliunas
Marius Miliunas

Reputation: 1063

CodeIgniter - get error messages

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

Upvotes: 2

Views: 2108

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25445

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.

Upvotes: 4

Related Questions