-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handling
If no route matches the request and no 404 handler is defined, dispatch()
throws PageNotFoundException.
Define a handler with setNotFoundHandler() to control the response instead. The
handler is a closure or controller reference, just like a route handler, and the
response is returned with status 404:
$router->setNotFoundHandler(function (Request $request, Response $response) {
$response->getBody()->write('Not Found');
return $response; // dispatch() returns this with a 404 status
});
// or a controller:
$router->setNotFoundHandler('ErrorController@notFound');
error_404()is a deprecated alias ofsetNotFoundHandler(), kept for backwards compatibility. See Upgrading 1.x → 2.0.
All router exceptions extend InitPHP\Router\Exception\RouterException (except
InvalidArgumentException). See Exceptions for the full hierarchy.
use InitPHP\Router\Exception\PageNotFoundException;
use InitPHP\Router\Exception\RouterException;
try {
$response = $router->dispatch();
} catch (PageNotFoundException $e) {
$response = $response->withStatus(404);
} catch (RouterException $e) {
$response = $response->withStatus(500);
}
(new Emitter())->emit($response);After dispatch(), you can inspect what handled the request:
$router->getCurrentController(); // controller class, '__CALLABLE__', or null
$router->getCurrentControllerMethod(); // method name, '' for closures, or null
$router->getCurrentArguments(); // the captured route argumentsNext: Caching.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration