Skip to content

Commit

Permalink
Updated 'error' function to handle not founds erros better. Also, fix…
Browse files Browse the repository at this point in the history
…ed server path issue.
  • Loading branch information
izniburak committed Jan 20, 2022
1 parent cb3be11 commit d2c1002
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 9 additions & 7 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
);
}
}

Expand Down Expand Up @@ -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]);
}
}
2 changes: 1 addition & 1 deletion src/RouterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/RouterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function __construct(string $message, int $statusCode = 500)
if (self::$debug) {
throw new Exception($message, $statusCode);
}
die("<h2>Opps! An error occurred.</h2> {$message}");
die($message);
}
}

0 comments on commit d2c1002

Please sign in to comment.