Skip to content

Commit d2c1002

Browse files
committed
Updated 'error' function to handle not founds erros better. Also, fixed server path issue.
1 parent cb3be11 commit d2c1002

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=7.2.5",
2121
"ext-json": "*",
22-
"symfony/http-foundation": "^5.1"
22+
"symfony/http-foundation": "^5.4.0"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^8.5 || ^9.4",

src/Router.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Router
4040
/**
4141
* Router Version
4242
*/
43-
const VERSION = '2.3.4';
43+
const VERSION = '2.4.0';
4444

4545
/**
4646
* @var string $baseFolder Pattern definitions for parameters of Route
@@ -326,14 +326,15 @@ public function run(): void
326326

327327
if ($foundRoute === false) {
328328
if (!$this->errorCallback) {
329-
$this->errorCallback = function () {
330-
$this->response()
331-
->setStatusCode(Response::HTTP_NOT_FOUND)
332-
->sendHeaders();
333-
return $this->exception('Looks like page not found or something went wrong. Please try again.');
329+
$this->errorCallback = function (Request $request, Response $response) {
330+
$response->setStatusCode(Response::HTTP_NOT_FOUND);
331+
$response->setContent('Looks like page not found or something went wrong. Please try again.');
332+
return $response;
334333
};
335334
}
336-
call_user_func($this->errorCallback);
335+
$this->routerCommand()->sendResponse(
336+
call_user_func($this->errorCallback, $this->request(), $this->response())
337+
);
337338
}
338339
}
339340

@@ -790,6 +791,7 @@ protected function getRequestUri(): string
790791
$dirname = $dirname === '/' ? '' : $dirname;
791792
$basename = basename($script);
792793
$uri = str_replace([$dirname, $basename], '', $this->request()->server->get('REQUEST_URI'));
794+
$uri = preg_replace('/\/'.str_replace(['.'],['\.'], $this->baseFolder).'/', '', $uri, 1);
793795
return $this->clearRouteName(explode('?', $uri)[0]);
794796
}
795797
}

src/RouterCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ protected function resolveMiddleware(string $middleware)
358358
*
359359
* @return Response|mixed
360360
*/
361-
protected function sendResponse($response)
361+
public function sendResponse($response)
362362
{
363363
if (is_array($response) || strpos($this->request->headers->get('Accept'), 'application/json') !== false) {
364364
$this->response->headers->set('Content-Type', 'application/json');

src/RouterException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function __construct(string $message, int $statusCode = 500)
2626
if (self::$debug) {
2727
throw new Exception($message, $statusCode);
2828
}
29-
die("<h2>Opps! An error occurred.</h2> {$message}");
29+
die($message);
3030
}
3131
}

0 commit comments

Comments
 (0)