BUGFIX: added error pages

This commit is contained in:
Jeroen De Meerleer 2021-08-06 12:30:00 +02:00
parent 9c7be573e0
commit 9e06a69f28
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 45 additions and 17 deletions

View File

@ -1,3 +1,9 @@
error:
path: '/error/{status}'
defaults:
_controller: JeroenED\Website\Controller\DefaultController::ErrorAction
status: '404'
default:
path: '/{page}'
defaults:

View File

@ -6,8 +6,10 @@ namespace JeroenED\Framework;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\UrlMatcher;
@ -21,21 +23,30 @@ class Router
public function route(Request $request, Kernel $kernel): Response
{
$requestContext = new RequestContext();
$this->requestContext = $requestContext->fromRequest($request);
$matcher = new UrlMatcher($this->routes, $this->requestContext);
$method = $matcher->match($request->getPathInfo());
$controller = explode('::', $method['_controller']);
$controllerObj = new ('\\' . $controller[0])($request, $kernel);
$action = $controller[1];
unset($method['_controller']);
unset($method['_route']);
$response = $controllerObj->$action(...$method);
try {
// Here be dragons
if ($response instanceof Response) {
return $response;
} else {
throw new InvalidArgumentException();
$requestContext = new RequestContext();
$this->requestContext = $requestContext->fromRequest($request);
$matcher = new UrlMatcher($this->routes, $this->requestContext);
$method = $matcher->match($request->getPathInfo());
$controller = explode('::', $method['_controller']);
$controllerObj = new ('\\' . $controller[0])($request, $kernel);
$action = $controller[1];
unset($method['_controller']);
unset($method['_route']);
$response = $controllerObj->$action(...$method);
if ($response instanceof Response) {
return $response;
} else {
throw new InvalidArgumentException();
}
} catch(ResourceNotFoundException $e) {
return new RedirectResponse($this->getUrlForRoute('error', ['status' => '404']));
} catch (\Throwable $e) {
return new RedirectResponse($this->getUrlForRoute('error', ['status' => (method_exists($e,'getStatusCode')) ? $e->getStatusCode() : '500']));
}
}

View File

@ -22,4 +22,16 @@ class DefaultController extends Controller
'nineties' => (isset($_COOKIE['nineties']))
], $page['status']);
}
public function ErrorAction($status)
{
$pageRepo = new Page();
$page = $pageRepo->getPage('error/' . $status);
return $this->render('/page.html.twig', [
'header' => $page['header'],
'content' => $page['content'],
'title' => $page['title'],
'nineties' => (isset($_COOKIE['nineties']))
], $status);
}
}

View File

@ -9,6 +9,7 @@ use GuzzleHttp\Client;
use JeroenED\Framework\Repository;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SSH2;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Page
{
@ -28,9 +29,7 @@ class Page
$return['content'] = file_get_contents(strtolower($this->root . '/' . $page . '.md'));
$return['status'] = '200';
} else {
$return['title'] = $titles['404'] ?? '';
$return['content'] = file_get_contents(strtolower($this->root . '/404.md'));
$return['status'] = '404';
throw new NotFoundHttpException();
}
return $return;
}