Implemented POST requests
This commit is contained in:
parent
bfadfbe721
commit
889f6ba8f0
|
@ -6,4 +6,10 @@ default:
|
||||||
login:
|
login:
|
||||||
path: '/login'
|
path: '/login'
|
||||||
defaults:
|
defaults:
|
||||||
_controller: JeroenED\Webcron\Controller\SecurityController::loginAction
|
_controller: JeroenED\Webcron\Controller\SecurityController::loginAction
|
||||||
|
|
||||||
|
login_check:
|
||||||
|
path: '/login_check'
|
||||||
|
methods: ['POST']
|
||||||
|
defaults:
|
||||||
|
_controller: JeroenED\Webcron\Controller\SecurityController::loginCheckAction
|
||||||
|
|
|
@ -28,6 +28,7 @@ abstract class Controller
|
||||||
{
|
{
|
||||||
return $this->kernel->getDbCon();
|
return $this->kernel->getDbCon();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
|
@ -54,4 +55,9 @@ abstract class Controller
|
||||||
$response = new Response($this->twig->render($template, $vars));
|
$response = new Response($this->twig->render($template, $vars));
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function generateRoute(string $route): string
|
||||||
|
{
|
||||||
|
return $this->kernel->getRouter()->getUrlForRoute($route);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@ class Kernel
|
||||||
private string $configDir;
|
private string $configDir;
|
||||||
private string $projectDir;
|
private string $projectDir;
|
||||||
private string $templateDir;
|
private string $templateDir;
|
||||||
|
private Router $router;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -59,7 +60,9 @@ class Kernel
|
||||||
public function getTemplateDir(): string
|
public function getTemplateDir(): string
|
||||||
{
|
{
|
||||||
return $this->templateDir;
|
return $this->templateDir;
|
||||||
}/**
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* @param string $templateDir
|
* @param string $templateDir
|
||||||
*/
|
*/
|
||||||
public function setTemplateDir(string $templateDir): void
|
public function setTemplateDir(string $templateDir): void
|
||||||
|
@ -67,12 +70,21 @@ class Kernel
|
||||||
$this->templateDir = $templateDir;
|
$this->templateDir = $templateDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Router
|
||||||
|
*/
|
||||||
|
public function getRouter(): Router
|
||||||
|
{
|
||||||
|
return $this->router;
|
||||||
|
}
|
||||||
|
|
||||||
public function handle(): Response
|
public function handle(): Response
|
||||||
{
|
{
|
||||||
$this->parseDotEnv($this->getProjectDir() . '/.env');
|
$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();
|
$request = $this->parseRequest();
|
||||||
return $this->createResponse($request, $routes);
|
return $this->router->route($request, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parseDotEnv(string $path): void
|
private function parseDotEnv(string $path): void
|
||||||
|
@ -81,36 +93,14 @@ class Kernel
|
||||||
$dotenv->loadEnv($path);
|
$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
|
private function parseRequest(): Request
|
||||||
{
|
{
|
||||||
return Request::createFromGlobals();
|
$request = Request::createFromGlobals();
|
||||||
|
return $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDbCon(): Connection
|
public function getDbCon(): Connection
|
||||||
{
|
{
|
||||||
return DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace JeroenED\Framework;
|
||||||
|
|
||||||
|
|
||||||
|
use http\Exception\InvalidArgumentException;
|
||||||
|
use Symfony\Component\Config\FileLocator;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Generator\UrlGenerator;
|
||||||
|
use Symfony\Component\Routing\Loader\YamlFileLoader;
|
||||||
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
|
||||||
|
class Router
|
||||||
|
{
|
||||||
|
private RouteCollection $routes;
|
||||||
|
private RequestContext $requestContext;
|
||||||
|
|
||||||
|
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];
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', true);
|
||||||
|
|
||||||
use JeroenED\Framework\Kernel;
|
use JeroenED\Framework\Kernel;
|
||||||
|
|
||||||
|
|
|
@ -16,4 +16,10 @@ class SecurityController extends Controller
|
||||||
}
|
}
|
||||||
return $this->render('security/login.html.twig');
|
return $this->render('security/login.html.twig');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function loginCheckAction(): Response
|
||||||
|
{
|
||||||
|
$_SESSION['isAuthenticated'] = true;
|
||||||
|
return new Response('Not yet implemented', 425);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -11,13 +11,13 @@
|
||||||
|
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
||||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
|
||||||
<script src="/public/js/site.js" type="text/javascript"></script>
|
<script src="/js/site.js" type="text/javascript"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="col-md-4 col-md-offset-4 col-xs-12">
|
<div class="col-md-4 col-md-offset-4 col-xs-12">
|
||||||
<h1>Webcron management</h1>
|
<h1>Webcron management</h1>
|
||||||
<form class="form-horizontal" method="post" action="/public/index.php">
|
<form class="form-horizontal" method="post" action="/login_check">
|
||||||
{% if not error == "" %}
|
{% if not error == "" %}
|
||||||
<div class="alert alert-danger fade in">
|
<div class="alert alert-danger fade in">
|
||||||
<a href="#" class="close" data-dismiss="alert">×</a>
|
<a href="#" class="close" data-dismiss="alert">×</a>
|
||||||
|
|
Loading…
Reference in New Issue