Welcome Guest, Not a member yet? Register   Sign In
404 page - more info, please
#1

[eluser]victorche[/eluser]
One of the changes in the changelog says something about more flexible and powerfull way of dealing with custom not found/404 error page.

Until now (in 2.0 beta core) I was dealing with this with MY_Router.php file.

Can someone give me an example ... of how can I make a custom 404 page (with a proper view, which looks like the rest of the design and offers links in the page for some main parts of the site).

With MY_Router.php I had really powerfull solution but if it is really an option in CI, I prefer not to extend the code and to keep it clean ...

Thanks in advance !

EDIT: Maybe we should wait for some updates in the user guide ... As I can see, it is still not complete and maybe it is a work in progress. There are even some missing parts, like:
http://ellislab.com/codeigniter/user-gui...lding.html
#2

[eluser]Twisted1919[/eluser]
Create a controller, make it load an error view, then give the controller name to the "404" route.
#3

[eluser]victorche[/eluser]
[quote author="Twisted1919" date="1296410771"]Create a controller, make it load an error view, then give the controller name to the "404" route.[/quote]
Thanks, but can you be a little bit more specific?

I want to be able to redirect to it, when ... for example I need a way to redirect to this 404 page, when there is no id in the database, for a news controller for example...
I mean, if I have /news/12 (existing news) - it will be displayed
But if I have /news/13f45qa (non existing id in the db) - then redirect to 404

I am making the check for a valid id in the controller and according to this - sending the user to 404
#4

[eluser]Twisted1919[/eluser]
What you don't understand is that, show_404(); is the function that is called automatically by codeigniter when the page isn't found.
Therefore, if you create a custom 404 page as i pointed in the previous post, it will be loaded when a user looks for a page that doesn't exists so you don't have to do additional steps.
Also, if you call show_404(); (note, means you do it manually by some reason) it will show same page .
#5

[eluser]Burak Erdem[/eluser]
[quote author="victorche" date="1296407257"]Maybe we should wait for some updates in the user guide ... As I can see, it is still not complete and maybe it is a work in progress. There are even some missing parts, like:
http://ellislab.com/codeigniter/user-gui...lding.html[/quote]

Scaffolding has been removed as of version 2, look at the change log http://ellislab.com/codeigniter/user-gui...gelog.html
#6

[eluser]boxyee[/eluser]
From what I understand, there is a new $route[‘404_override’] config item, which lets you set the controller to redirect to when a controller isn't found.

Eg.
If $route[‘404_override’] is set to 'custom-error',
then when you browse to
domain.com/invalid-controller, the custom-error controller is loaded.

You could use this in some sort of CMS to load a page dependant on the URL or just as your own errors controller.
#7

[eluser]skunkbad[/eluser]
This is the way I've been handling errors. Works for more than just 404:

MY_Exceptions:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (stristr($_SERVER['HTTP_HOST'], 'localhost' ))
{
    define('BASE', 'http://' . DEVELOPMENT_SERVER_HOSTNAME);
}
else
{
    define('BASE', 'http://' . PRODUCTION_SERVER_HOSTNAME);
}

class MY_Exceptions extends CI_Exceptions {

    function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        set_status_header($status_code);

        // First context options are built for the xampp development server. It was not compiled --with-curlwrappers.
        if (BASE == 'http://' . DEVELOPMENT_SERVER_HOSTNAME)
        {
            $opts = array(
              'http'=>array(
                'method'=>"POST",
                'header'=>    "Content-type: application/x-www-form-urlencoded\r\n" ,
                'user_agent'=>    $_SERVER['HTTP_USER_AGENT'],
                'content' => http_build_query( array ( 'heading' => $heading,
                                                        'message' => $message,
                                                        'status_code' => $status_code
                                                    ),
                                                    '',
                                                    '&'
                                            )
              )
            );
        // or else if the script is running from the production server, the options are built as needed (with the header options as an array)
        }
        else
        {
            $opts = array(
              'http'=>array(
                'method'=>"POST",
                'header'=>    array(    
                                    'Content-type: application/x-www-form-urlencoded'
                                ),
                'user_agent'=>    $_SERVER['HTTP_USER_AGENT'],
                'content' => http_build_query( array ( 'heading' => $heading,
                                                        'message' => $message,
                                                        'status_code' => $status_code
                                                    )
                                            )
              )
            );
        }
        $context = stream_context_create($opts);
        return file_get_contents( BASE .'/errorpage', FALSE , $context);
    }

}

errorpage controller:
Code:
<?php
class Errorpage extends MY_Controller {

    public function index()
    {
        if($this->input->post('heading'))
        {
            $vars['title'] = $this->input->post('heading');
            $vars['heading'] = $this->input->post('heading');
        }
        else
        {
            $vars['title'] = '404 - PAGE NOT FOUND';
            $vars['heading'] = '404 - PAGE NOT FOUND';
        }
        if($this->input->post('message'))
        {
            $vars['message'] = $this->input->post('message');
        }
        else
        {
            $vars['message'] = 'The page you tried to access does not exist or has been moved.';
        }
        if($this->input->post('status_code'))
        {
            $vars['status_code'] = $this->input->post('status_code');
        }
        else
        {
            $vars['status_code'] = '404';
        }
        $this->load->vars($vars);

        // insert page specific data into template
        $data = array(
            'content' => $this->load->view('errorpage', '', TRUE ),
            'dynamic_extras' => $this->load->view('countdown', '', TRUE)
        );
        $this->load->view('template_content', $data );
    }
}

errorpage view:
Code:
<div id="single-column">
    <h2>&lt;?php echo $heading; ?&gt;</h2>
    <p>&lt;?php echo $message; ?&gt;</p>
    <p>You will be redirected back to the home page in <span id="countdown" style="color:red; font-weight:bold;">15</span> seconds<br />unless you have disabled javascript.</p>
    <p>Click <a href="&lt;?php echo base_url(); ?&gt;">HERE</a> if you don't want to wait.</p>
</div>

I'd love to know if there is a better/easier way to handle ALL errors!
#8

[eluser]victorche[/eluser]
Thanks for the info, but... why when I have a custom 404, and I am using the option to override the default 404... Then my libraries are not autoloaded. Why is this and how can I avoid it?
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Error::$template

Filename: controllers/error.php

Line Number: 15
My template library is autoloaded in the config files... So I really have no idea why this is not working Sad
#9

[eluser]cominus[/eluser]
Victorche - here is what I did:

In applications/config/routes/ approx line 42:
Code:
$route['404_override'] = 'errors/page_missing';

new file applications/controllers/errors.php:
Code:
&lt;?php
class Errors extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    function page_missing()
    {
        $data['page_title'] = 'Error 404';
        $data['page_heading'] = 'The page you are looking for is missing!';
        $data['webmaster_email'] = '[email protected]';
        $this->load->view('header', $data);
        $this->load->view('page_missing', $data);
        $this->load->view('footer');
    }

    function forbidden()
    {
        $data['page_title'] = 'Error 403';
        $data['page_heading'] = 'Directory access is forbidden!';
        $this->load->view('header', $data);
        $this->load->view('forbidden', $data);
        $this->load->view('footer');
    }
}

new file applications/views/page_missing.php:
Code:
<div id="content-1col">

    <h1>&lt;?php echo $page_heading;?&gt;</h1>
    
    <p>Sorry! We have looked through the files but cannot find the URL you typed in.
    <br />Please check the menu, or the search box.
    <br />If that does not work, please contact the webmaster at &lt;?php echo safe_mailto($webmaster_email, '[Click Here to Contact]');?&gt;.</p>

</div>

The page_missing custom error page overrides the stock CI error message for 404.

The only problem I have (which is located at thread http://ellislab.com/forums/viewthread/181342/ ) is the forbidden option is not working - the custom 404 page missing error page is presented there also. So, that is a start.

Hope this is helpful.
#10

[eluser]William Rufino[/eluser]
What do I do if I need to invoke show_404 ?




Theme © iAndrew 2016 - Forum software by © MyBB