CodeIgniter Forums
[SOLVED] 404 Page Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: [SOLVED] 404 Page Controller (/thread-16989.html)



[SOLVED] 404 Page Controller - El Forum - 03-22-2009

[eluser]DougW[/eluser]
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?


[SOLVED] 404 Page Controller - El Forum - 03-23-2009

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

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


[SOLVED] 404 Page Controller - El Forum - 03-23-2009

[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.


errors/error_404.php
Code:
<?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.    
    $CI->output->showData($content);

} else {


    /*
        OLD WAY. Use this if there is not an instance of CI
    */
    
    
    ?&gt;
    &lt;html&gt;
    &lt;head&gt;
    &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;
    }
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <div id="content">
            <h1>&lt;?php echo $heading; ?&gt;</h1>
            &lt;?php echo $message; ?&gt;
        </div>
    &lt;/body&gt;
    &lt;/html&gt;
    
&lt;? } ?&gt;



[SOLVED] 404 Page Controller - El Forum - 04-12-2009

[eluser]vitch[/eluser]
I just wrote up my solution to this problem - maybe it will help?

http://www.kelvinluck.com/2009/04/custom-404-error-messages-with-codeigniter/

Cheers,

Kelvin Smile


[SOLVED] 404 Page Controller - El Forum - 05-15-2009

[eluser]davidbehler[/eluser]
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!


[SOLVED] 404 Page Controller - El Forum - 05-15-2009

[eluser]vitch[/eluser]
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:

http://ellislab.com/forums/viewthread/109892/P15/#576973

Hope it helps,

Kelvin Smile


[SOLVED] 404 Page Controller - El Forum - 05-16-2009

[eluser]tkyy[/eluser]
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.


[SOLVED] 404 Page Controller - El Forum - 05-17-2009

[eluser]vitch[/eluser]
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...


[SOLVED] 404 Page Controller - El Forum - 01-15-2010

[eluser]DieterStruik[/eluser]
Possible solution