We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Lazy Load Not-Found Handler?

Is there a way to lazy load the Not-Found handler? In the Docs all I see is directly using $app->notFound with an anonymous function, however, if I want perhaps more complex code for my 404 pages, I'd like to hand it off to a 404 controller or something similar.

For my other handlers I lazy load them as shown in the docs:

// Users handler
$users = new MicroCollection();
$users->setHandler("UsersController", true);
$users->setPrefix("/users");
$users->get("/get/{id}", "get");
$users->get("/add/{payload}", "add");
$app->mount($users);

Is there a parallel way to do this with the Not-Found handler? Does it have an associated prefix or something?



79.0k
Accepted
answer
edited Jul '17

Well, not found handler is kind of special case. Name says it all. Why would you lazy load that one too? Due to perforamnce implications? You really should not bother with that.

I have fairly complex error controller, which acts as global catcher for all exceptions/errors, and as well for 404's:

/**
 * Not found handler
 */
$app->notFound(function () {
    ErrorHandler::stopMiddleware(404, 'RESOURCE_NOT_FOUND : The server has not found anything matching the Request-URI.', 0xff);
});

/**
 * Exception handler
 */
$app->error(function (\Exception $e = null) use ($app) {
    $catchable = new ErrorHandler($app->persistent->auditID); //injecting runtime value
    return $catchable->err($e);
});

Is it important to lazy load all your app logic - controllers, there you expect heavy code.