Set the correct HTTP response code for the router

Sets the correct HTTP response code when the route path is not
found or the requested method is not allowed.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2019-08-26 12:39:40 -03:00
parent 4077c61b2e
commit ae969ecd39

View File

@ -9,6 +9,7 @@ declare(strict_types=1);
use FastRoute\Dispatcher;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use function FastRoute\simpleDispatcher;
@ -16,7 +17,7 @@ if (! defined('ROOT_PATH')) {
define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
}
global $route;
global $containerBuilder, $route;
require_once ROOT_PATH . 'libraries/common.inc.php';
@ -41,11 +42,17 @@ $routeInfo = $dispatcher->dispatch(
rawurldecode($route)
);
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(404);
Message::error(sprintf(
__('Error 404! The page %s was not found.'),
'<code>' . ($route) . '</code>'
))->display();
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var Response $response */
$response = $containerBuilder->get(Response::class);
$response->setHttpResponseCode(405);
Message::error(__('Error 405! Request method not allowed.'))->display();
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
$handler = $routeInfo[1];