diff --git a/config/routes.yaml b/config/routes.yaml index e8da168..2429ab4 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -6,4 +6,10 @@ default: login: path: '/login' defaults: - _controller: JeroenED\Webcron\Controller\SecurityController::loginAction \ No newline at end of file + _controller: JeroenED\Webcron\Controller\SecurityController::loginAction + +login_check: + path: '/login_check' + methods: ['POST'] + defaults: + _controller: JeroenED\Webcron\Controller\SecurityController::loginCheckAction diff --git a/lib/Framework/Controller.php b/lib/Framework/Controller.php index 6116bd6..48c6e8f 100644 --- a/lib/Framework/Controller.php +++ b/lib/Framework/Controller.php @@ -28,6 +28,7 @@ abstract class Controller { return $this->kernel->getDbCon(); } + /** * @return Request */ @@ -54,4 +55,9 @@ abstract class Controller $response = new Response($this->twig->render($template, $vars)); return $response; } + + public function generateRoute(string $route): string + { + return $this->kernel->getRouter()->getUrlForRoute($route); + } } \ No newline at end of file diff --git a/lib/Framework/Kernel.php b/lib/Framework/Kernel.php index 31fbfb9..4cc1126 100644 --- a/lib/Framework/Kernel.php +++ b/lib/Framework/Kernel.php @@ -20,6 +20,7 @@ class Kernel private string $configDir; private string $projectDir; private string $templateDir; + private Router $router; /** * @return string @@ -59,7 +60,9 @@ class Kernel public function getTemplateDir(): string { return $this->templateDir; - }/** + } + + /** * @param string $templateDir */ public function setTemplateDir(string $templateDir): void @@ -67,12 +70,21 @@ class Kernel $this->templateDir = $templateDir; } + /** + * @return Router + */ + public function getRouter(): Router + { + return $this->router; + } + public function handle(): Response { $this->parseDotEnv($this->getProjectDir() . '/.env'); - $routes = $this->parseRoutes($this->getConfigDir(), 'routes.yaml'); + $this->router = new Router(); + $this->router->parseRoutes($this->getConfigDir(), 'routes.yaml'); $request = $this->parseRequest(); - return $this->createResponse($request, $routes); + return $this->router->route($request, $this); } private function parseDotEnv(string $path): void @@ -81,36 +93,14 @@ class Kernel $dotenv->loadEnv($path); } - private function parseRoutes(string $dir, string $file): RouteCollection - { - $routeloader = new YamlFileLoader(new FileLocator($dir)); - return $routeloader->load($file); - } - private function parseRequest(): Request { - return Request::createFromGlobals(); + $request = Request::createFromGlobals(); + return $request; } public function getDbCon(): Connection { return DriverManager::getConnection(['url' => $_ENV['DATABASE']]); } - - private function createResponse($request, $routes): Response - { - $requestContext = RequestContext::fromUri($request->getUri()); - $matcher = new UrlMatcher($routes, $requestContext); - $method = $matcher->match($request->getPathInfo()); - $controller = explode('::', $method['_controller']); - $controllerObj = new ('\\' . $controller[0])($request, $this); - $action = $controller[1]; - $response = $controllerObj->$action(); - - if ($response instanceof Response) { - return $response; - } else { - throw new InvalidArgumentException(); - } - } } \ No newline at end of file diff --git a/lib/Framework/Router.php b/lib/Framework/Router.php new file mode 100644 index 0000000..92ae719 --- /dev/null +++ b/lib/Framework/Router.php @@ -0,0 +1,51 @@ +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]; + $response = $controllerObj->$action(); + + if ($response instanceof Response) { + return $response; + } else { + throw new InvalidArgumentException(); + } + } + + public function parseRoutes(string $dir, string $file): void + { + $routeloader = new YamlFileLoader(new FileLocator($dir)); + $this->routes = $routeloader->load($file); + } + + public function getUrlForRoute(string $route, array $params = []): string + { + $matcher = new UrlGenerator($this->routes, $this->requestContext); + return $matcher->generate($route, $params, UrlGenerator::ABSOLUTE_URL); + } +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 115cc56..149c5a1 100644 --- a/public/index.php +++ b/public/index.php @@ -1,4 +1,6 @@ render('security/login.html.twig'); } + + public function loginCheckAction(): Response + { + $_SESSION['isAuthenticated'] = true; + return new Response('Not yet implemented', 425); + } } \ No newline at end of file diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig index 5f2f430..a944f43 100644 --- a/templates/security/login.html.twig +++ b/templates/security/login.html.twig @@ -11,13 +11,13 @@ - +

Webcron management

-
+ {% if not error == "" %}
×