Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] 404 Page Controller

I have implemented a fairly sophisticated site and want the 404 pages to be drawn in the same wrapper as the rest of the site preserving header and footer. In order to avoid duplicate code, I want to include the header and footer views within the error404.php page. Is there a way to redirect or something so that I can call a controller to then load the 404 page, thus setting all the variables I need for the header and footer before plugging in the 404 message?

This depends on your routes setup.
You could add a last route:
$route[':any'] = 'your_error_controller';

If that's not possible for you, you'd have to extend the Router class and modify _validate_request().

[eluser]louis w[/eluser]
Here is what I am using to achieve this. Pretty straight forward. Just check to make sure the CI object is around, and if so set a variable and call it just like you would with $this->

Don't use this for error_general, it requires some additional checking to make sure there are not recursive errors.

<?php header("HTTP/1.1 404 Not Found");

if (function_exists('get_instance')) {

    $CI= &get;_instance();
    $content     = '<div class="error_message">'
                . '<h1>'.$heading.'</h1>'
                . $message
                . '</div>';
    // This is my own output wrapper/templating class. You could pass to a view instead.    

} else {

        OLD WAY. Use this if there is not an instance of CI
    &lt;title&gt;404 Page Not Found&lt;/title&gt;
    &lt;style type="text/css"&gt;
    body {
    background-color:    #fff;
    margin:                40px;
    font-family:        Lucida Grande, Verdana, Sans-serif;
    font-size:            12px;
    color:                #000;
    #content  {
    border:                #999 1px solid;
    background-color:    #fff;
    padding:            20px 20px 12px 20px;
    h1 {
    font-weight:        normal;
    font-size:            14px;
    color:                #990000;
    margin:             0 0 4px 0;
        <div id="content">
            <h1>&lt;?php echo $heading; ?&gt;</h1>
            &lt;?php echo $message; ?&gt;
&lt;? } ?&gt;

I just wrote up my solution to this problem - maybe it will help?



Kelvin Smile

Hey vitch,
I tried to implement your solution but doesn't work for me.
Seems like you need the curl library and as far as I can tell it's not available on my system.
I was looking for a solution I could ship out with my blogging software and therefor I need something that runs out of the box.

That's why the next version of my DBlog will be implementing louis solution that worked right away! Smile

Thx for that!

Hey waldmeister Smile

Yeah - I've recently started using something similar too but haven't had a chance to update my blog post yet... I posted a little more info in this other thread:


Hope it helps,

Kelvin Smile

i usually just put a header redirect in the error file to a controller and just throw the 404 header inside of the view, display page as normal.

ive modified the core of one of my projects for ci to recognize the 404 as a viewfile (404.php, if it exists) which worked very well, consider doing that.

The problem with just redirecting to another controller are discussed in the thread linked above. Basically it changes the URL in the user's browser which is incorrect behaviour...

Possible solution

Theme © iAndrew 2016 - Forum software by © MyBB