1

I know how to make response routes in the actual /config/routes.php file, but I can't find where to change the default 'fetal dispatcher' error. I'd like to be able to have it route to a nice 404 page I've made when there's a missing page/action controller. Is that possible?

keithp
  • 352
  • 1
  • 4
  • 13
  • see [404'd! Custom errors](http://gavd.github.io/step-by-step-web-apps-with-lithium-php/404d.html) –  May 30 '16 at 10:18

1 Answers1

2

Yes, you can take advantage of lithium\core\ErrorHandler for this. See the code in the default config/bootstrap/errors.php:

ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
    $response = new Response(array(
        'request' => $params['request'],
        'status' => $info['exception']->getCode()
    ));

    Media::render($response, compact('info', 'params'), array(
        'library' => true,
        'controller' => '_errors',
        'template' => 'development',
        'layout' => 'error',
        'request' => $params['request']
    ));
    return $response;
});

This is saying, if any exception occurs during Dispatcher::run(), display the development.html.php template from the views/_errors folder with the layouts/error.html.php layout.

So you can change that -- maybe you check the Environment to see if this is a dev or production environment and display a different template for production.

Maybe if $info['exception']->getCode() === 404, you can switch to a template specifically for 404 errors.

rmarscher
  • 5,596
  • 2
  • 28
  • 30
  • 2
    Alternatively, you should be able to pass `['code' => 404]` as the second parameter. – Nate Abele Apr 16 '14 at 10:32
  • OH perfect! I knew Lithium had something build in but I'm still learning it. Thank you very much – keithp Apr 16 '14 at 12:14
  • @rmarscher - would the above code will catch the fatal error in my application? I don't want show blank page instead I would like to perform some other activity. I am trying to locate if ErrorHandler::apply will become effective when it's fatal error... please guide – user269867 Apr 22 '14 at 07:27
  • @user269867 I think you will need to use register_shutdown_function() to capture fatal errors. I'll update my response with an example when I have a moment. – rmarscher Apr 26 '14 at 20:27