views:

92

answers:

3

Hello everybody,

In my Bootstrap.php I've deactivated the Profiler (or is it better to be activated?), and the Errors.

Now if somebody is calling an Url, maybe: /notexist, and there is no action_notexist(), the Site is blank.

My Question: How can I create a main Error Template, which should be loaded instead of the white page. E.g. If you call: http://twitter.com/notexistinguser, there is a "Page does not exist" Error, the same with Kohana3?

Thanks :)

+2  A: 

Don't ignore exceptions, catch them.

shadowhand
For an example of catching exceptions see http://kerkness.ca/wiki/doku.php?id=routing:404_pages_by_catching_reflection_exception
slacker
nice @slacker =) Looks interesting.. Thanks!
ahmet2106
+1  A: 

What you need to do is catch the Kohana_Exception in your bootstrap.php file. Here's a code sample from one of my projects.

try
{
    echo Request::instance()
        ->execute()
        ->send_headers()
        ->response;
}
catch (Kohana_Exception $e)
{
    echo Request::factory('static/404')->execute()->send_headers()->response;
}

I'll explain what's going on here. If a route doesn't exist for the URL requested then it throws a Request_Exception (instance of Kohana_Exception).

I then use the HMVC feature to create a sub-request to the 404 page which deals with the template, status codes, logging and error messages.

In the future Kohana might have a special exception which deals with responses, but for now you can use my solution.

Hope that helped you out.

The Pixel Developer
an interesting way, I will try it... This are the end lines of the bootstrap? Am I right? Thank you
ahmet2106
Yes, you are correct.
The Pixel Developer
+1  A: 

I'm new to Kohana but I use the following technique. First, define some constant, for example IN_PRODUCTION:

define('IN_PRODUCTION', true);

Second, create new Exception class, for example Exception_404 that inherits Kohana_Exception. Third, replace this code:

echo Request::instance()
->execute()
->send_headers()
->response;

with following:

$request = Request::instance();
try 
{
    $request->execute();
}
catch(Exception_404 $e)
{
    if ( ! IN_PRODUCTION)
    {
        throw $e;
    }

    //404 Not Found
    $request->status = 404;
    $request->response = View::factory('404');
}

print $request->send_headers()->response;

Now you have your own error template. Is that what you want?

franzose
nice, thank you, i will test it this week :)
ahmet2106
The Pixel Developer
ah, I didn't know that. So, it's fine, thanks a lot :)
franzose