开发者

Kohana3 - Error Template if errors = FALSE

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: /notexis开发者_高级运维t, 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 :)


Don't ignore exceptions, catch them.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜