Welcome Guest, Not a member yet? Register   Sign In
404 Errors
#1

[eluser]omed habib[/eluser]
Is there a prebuilt standard method for custom 404 pages? How do I have CI redirect 404 errors to a custom controller/function rather than the standard error_404.php?

Thanks
#2

[eluser]Avatar[/eluser]
use redirect() in CI
#3

[eluser]Vince Stross[/eluser]
Read this page in the User Guide for some good information on CI's built in 404 handling, etc.

http://ellislab.com/codeigniter/user-gui...rrors.html
#4

[eluser][email protected][/eluser]
if you are doing a redirect, how do you pass the actual message onto the custom error page?

Also, how does CI do it today? ie. if you type in http://codeigniter.com/jibberishlkjslkjf then you are forwarded to a custom 404 page (the headers and footer looks the same); thus, it displays '404' but still keeps the url the same "http://codeigniter.com/jibberishlkjslkjf"? How could we write something like that?
#5

[eluser]omed habib[/eluser]
Great question, did you find an answer yet?
#6

[eluser]TheFuzzy0ne[/eluser]
Can you not extend the CI_Exceptions class in /system/libraries/Exception.php and then override the show_404 method?

Alternatively, you can rewrite the error_404.php file to do what you want it to. I would suggest that you don't pass it to another controller however, as that has already been done. You would need to write a library or some helper functions to do what you want.
#7

[eluser][email protected][/eluser]
I figured this out and implemented it. Let me know if you need the solution.

Basically, there are two options where an error can be called. 1) inside the controller (ie. during a calculation) or 2) before the controller (ie. pre-controller hook)

So the exceptions class is overriden and a check is performed whether a &get;_instance() exists.
#8

[eluser]meade[/eluser]
I had the same issue - I just changed my website and wanted the people to go to MY 404 page instead of the standard error page, so I modified the system/applications/errors/error_404.php page to:
<?
header( 'Location: http://www.yoursite.org/yourcontroller/your404function') ;
?>
which routed the 404's to my specific controller/function which then allowed me to show my 404 page (remember with header nothing before nothing after the line.
#9

[eluser][email protected][/eluser]
Keep in mind that you are doing a redirect, and the page name will change to /yourcontroller/your404function...which I found to be a pain.....I wanted to display the 404 error but keep the url the same /originalcontroller/normal_function....not 404....
#10

[eluser]wr5aw[/eluser]
I have a simple 'pages' controller that uses a combination of htaccess and _remap() to create a custom 404 page. In my controller, I do this:
Code:
function _remap($page='index')
    {
        $file = str_replace('/', DS, APPPATH."/views/page/$page.php");
        if ( !file_exists($file) )
        {
            $this->output->set_status_header('404');
            $data['content'] = $this->load->view('error404', '', TRUE);
        }
        else
        {
            $data['content'] = $this->load->view("page/$page", $data, TRUE);
        }
        $this->load->view('page/template', $data);
    }
The views/page/error404.php might look something like this:
Code:
<h2>Oops... I couldn't find that page</h2>
<p>Check the documentation</p>
And the views/page/template.php might look something like this:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Page Controller&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo $content; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
There's more going on in the controller besides this but that should give you an idea of how I handle it.




Theme © iAndrew 2016 - Forum software by © MyBB