diff --git a/composer.json b/composer.json index ff50399..b6e4775 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": ">=7.2.5", "ext-json": "*", - "symfony/http-foundation": "^5.1" + "symfony/http-foundation": "^5.4.0" }, "require-dev": { "phpunit/phpunit": "^8.5 || ^9.4", diff --git a/src/Router.php b/src/Router.php index fd623e5..6d3f54e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -40,7 +40,7 @@ class Router /** * Router Version */ - const VERSION = '2.3.4'; + const VERSION = '2.4.0'; /** * @var string $baseFolder Pattern definitions for parameters of Route @@ -326,14 +326,15 @@ public function run(): void if ($foundRoute === false) { if (!$this->errorCallback) { - $this->errorCallback = function () { - $this->response() - ->setStatusCode(Response::HTTP_NOT_FOUND) - ->sendHeaders(); - return $this->exception('Looks like page not found or something went wrong. Please try again.'); + $this->errorCallback = function (Request $request, Response $response) { + $response->setStatusCode(Response::HTTP_NOT_FOUND); + $response->setContent('Looks like page not found or something went wrong. Please try again.'); + return $response; }; } - call_user_func($this->errorCallback); + $this->routerCommand()->sendResponse( + call_user_func($this->errorCallback, $this->request(), $this->response()) + ); } } @@ -790,6 +791,7 @@ protected function getRequestUri(): string $dirname = $dirname === '/' ? '' : $dirname; $basename = basename($script); $uri = str_replace([$dirname, $basename], '', $this->request()->server->get('REQUEST_URI')); + $uri = preg_replace('/\/'.str_replace(['.'],['\.'], $this->baseFolder).'/', '', $uri, 1); return $this->clearRouteName(explode('?', $uri)[0]); } } diff --git a/src/RouterCommand.php b/src/RouterCommand.php index c3c1903..abfa2b4 100644 --- a/src/RouterCommand.php +++ b/src/RouterCommand.php @@ -358,7 +358,7 @@ protected function resolveMiddleware(string $middleware) * * @return Response|mixed */ - protected function sendResponse($response) + public function sendResponse($response) { if (is_array($response) || strpos($this->request->headers->get('Accept'), 'application/json') !== false) { $this->response->headers->set('Content-Type', 'application/json'); diff --git a/src/RouterException.php b/src/RouterException.php index a029825..0f03148 100644 --- a/src/RouterException.php +++ b/src/RouterException.php @@ -26,6 +26,6 @@ public function __construct(string $message, int $statusCode = 500) if (self::$debug) { throw new Exception($message, $statusCode); } - die("