Currently the router throws a RouteNotFoundException either if it doesn't find a route with a matching pattern or a matching method. My suggestion is to introduce either a property on RouteNotFoundException or a new MethodMismatchException to differentiate between a 404 Not Found and a 405 Method Not Allowed error.
Example:
<?php
use MiladRahimi\PhpRouter\Router;
use Laminas\Diactoros\Response\HtmlResponse;
use MiladRahimi\PhpRouter\Exceptions\RouteNotFoundException;
require __DIR__ . "/vendor/autoload.php";
$router = Router::create();
$router->get('/', function() use ($twig) {
return new HtmlResponse("<h1>Test</h1>\n");
});
try {
$router->dispatch();
} catch (RouteNotFoundException $ex) {
// We have no way to tell if it really is a 404 or 405 error...
$router->getPublisher()->publish(new HtmlResponse('Not found.', 404));
} catch (\Throwable $e) {
$router->getPublisher()->publish(new HtmlResponse('Internal error.', 500));
}
If we POST http://<DOMAIN>/ we get a HTTP/1.1 404 Not Found, despite it actually resembeling a 405 error.
Currently the router throws a
RouteNotFoundException
either if it doesn't find a route with a matching pattern or a matching method. My suggestion is to introduce either a property onRouteNotFoundException
or a newMethodMismatchException
to differentiate between a404 Not Found
and a405 Method Not Allowed
error.Example:
If we
POST http://<DOMAIN>/
we get aHTTP/1.1 404 Not Found
, despite it actually resembeling a 405 error.Proposal:
If we now
POST http://<DOMAIN>/
we correctly get aHTTP/1.1 405 Method Not Allowed
.