Closed mbamber1986 closed 3 years ago
As far as i understood you pointed to three concepts: route parameter patterns, error handling and 404 errors.
If you want to declare a route parameter to limit the parameter value you can see the following example:
// id must be a number!
// It raises RouteNotFoundException if user enter non-numeric value for id
$router->pattern('id', '[0-9]+');
$router->get('/post/{id}', function (int $id) {
return 'Content of the post: ' . $id;
});
You can raise any exceptions in your code/controllers/middleware and catch them like this:
$router->get('/', function () {
throw new MyCustomException();
});
try {
$router->dispatch();
} catch (MyCustomException $e) {
// Log and report the error...
$router->getPublisher()->publish(new HtmlResponse('Internal error.', 500));
}
As for 404 pages you should catch RouteNotFoundException
like this:
$router->get('/', function () {
return 'Home.';
});
try {
$router->dispatch();
} catch (RouteNotFoundException $e) {
// If compiler is here, it means user wants a page that does not exist
// Show your 404 page or use something like this:
$router->getPublisher()->publish(new HtmlResponse('Not found.', 404));
}
Thank you for your reply I will try that I think that should work thanks again
On Sun, 7 Feb 2021, 08:42 Milad Rahimi, notifications@github.com wrote:
As far as i understood you pointed to three concepts: route parameter patterns, error handling and 404 errors.
If you want to declare a route parameter to limit the parameter value you can see the following example:
// id must be a number!// It raises RouteNotFoundException if user enter non-numeric value for id$router->pattern('id', '[0-9]+'); $router->get('/post/{id}', function (int $id) { return 'Content of the post: ' . $id; });
You can raise any exceptions in your code/controllers/middleware and catch them like this:
$router->get('/', function () { throw new MyCustomException(); }); try { $router->dispatch(); } catch (MyCustomException $e) { // Log and report the error... $router->getPublisher()->publish(new HtmlResponse('Internal error.', 500)); }
As for 404 pages you should catch RouteNotFoundException like this:
$router->get('/', function () { return 'Home.'; }); try { $router->dispatch(); } catch (RouteNotFoundException $e) { // If compiler is here, it means user wants a page that does not exist // Show your 404 page or use something like this: $router->getPublisher()->publish(new HtmlResponse('Not found.', 404)); }
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/miladrahimi/phprouter/issues/30#issuecomment-774635851, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHKAGFDTPVKMY4IA4BEKVQTS5ZG5PANCNFSM4XG2PMWQ .
I want to say thanks for your reply the other day i have finally tried the code you provded and it works a charm
You can star the repository if you liked it ;)
I think I already have :)
On Wed, 10 Feb 2021, 11:38 Milad Rahimi, notifications@github.com wrote:
You can star the repository if you liked it ;)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/miladrahimi/phprouter/issues/30#issuecomment-776648741, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHKAGFAOXKKEEXDPF5WLSDDS6JV4ZANCNFSM4XG2PMWQ .
Hi I was wondering is there a way in your code to stop it giving the scripts error message for example if i tell it i was int values only on the routing and i type string i get uncaught errors anyway i can formward this to a 404 page etc