Removed the header() calls from the class to add more flexibility to 404 and 405 routes

This commit is contained in:
SteamPixel 2020-01-11 22:18:06 +01:00
parent 8e5fb1157a
commit 6b9c6bbf6d
2 changed files with 8 additions and 2 deletions

View File

@ -93,12 +93,10 @@ class Route {
if (!$route_match_found) {
// But a matching path exists
if ($path_match_found) {
header('HTTP/1.0 405 Method Not Allowed');
if (self::$methodNotAllowed) {
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
}
} else {
header('HTTP/1.0 404 Not Found');
if (self::$pathNotFound) {
call_user_func_array(self::$pathNotFound, Array($path));
}

View File

@ -125,6 +125,10 @@ Route::add('/this-route-is-defined', function() {
// Add a 404 not found route
Route::pathNotFound(function($path) {
// Do not forget to send a status header back to the client
// The router will not send any headers by default
// So you will have the full flexibility to handle this case
header('HTTP/1.0 404 Not Found');
navi();
echo 'Error 404 :-(<br>';
echo 'The requested path "'.$path.'" was not found!';
@ -132,6 +136,10 @@ Route::pathNotFound(function($path) {
// Add a 405 method not allowed route
Route::methodNotAllowed(function($path, $method) {
// Do not forget to send a status header back to the client
// The router will not send any headers by default
// So you will have the full flexibility to handle this case
header('HTTP/1.0 405 Method Not Allowed');
navi();
echo 'Error 405 :-(<br>';
echo 'The requested path "'.$path.'" exists. But the request method "'.$method.'" is not allowed on this path!';