I'm using klein.php router, which is really powerful, But Now I want to handle exceptions, so from the documentations I'm using this function
$klein->onHttpError(function ($code, $router,$matched,$method_matched,$http_exception) {
switch ($code) {
case 404:
$router->response()->body(
'404 Page'
);
break;
case 405:
$router->response()->body(
'You can\'t do that!'
);
break;
default:
$router->response()->body(
'Oh no, a bad error happened that caused a '. $code
);
}
});
But even if there is no exception, this function is rendering and printing 404 page
in the end of every page.
Am I doing something wrong? I tried checking $matched
but no luck.
And I want to use $service->render()
in error handling, How can I do that? Because service is not accessible in particular onHttpError
function.
After experimenting, I find out that it is occurring due to skipRemaining()
function. Then I started to exploring the issues again. Issue #285 provided a solution.
But still I didn't find anyway to render a page in onHttpError()
function using $service
object call.
Edit:
To access $service
object in onHttpError()
, We have to declare it again. So the code will be
$klein->onHttpError(function ($code, $router) {
$service = $router->service();
$service->render('your-page-url.php');
});