microserviciile de register, login finalizate
This commit is contained in:
Regular → Executable
+47
-49
@@ -1,50 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
use app\core\exceptions\NotFoundException;
|
||||
|
||||
class Router
|
||||
{
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
protected array $routes = [];
|
||||
|
||||
public function __construct(Request $request, Response $response)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function get($path, $callback)
|
||||
{
|
||||
$this->routes['get'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function post($path, $callback)
|
||||
{
|
||||
$this->routes['post'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function resolve()
|
||||
{
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->method();
|
||||
$callback = $this->routes[$method][$path] ?? false;
|
||||
|
||||
if (!$callback) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
$controller = new $callback[0]();
|
||||
Application::$app->controller = $controller;
|
||||
$controller->action = $callback[1];
|
||||
$callback[0] = $controller;
|
||||
|
||||
foreach ($controller->getMiddlewares() as $middleware) {
|
||||
$middleware->execute();
|
||||
}
|
||||
|
||||
return call_user_func($callback, $this->request, $this->response);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
use app\exceptions\PageNotFoundException;
|
||||
|
||||
class Router
|
||||
{
|
||||
public Request $request;
|
||||
protected array $routes = [];
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function get($path, $callback): void
|
||||
{
|
||||
$this->routes['GET'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function post($path, $callback): void
|
||||
{
|
||||
$this->routes['POST'][$path] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws PageNotFoundException
|
||||
*/
|
||||
public function resolve()
|
||||
{
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->method();
|
||||
$callback = $this->routes[$method][$path] ?? false;
|
||||
|
||||
if (!$callback) {
|
||||
throw new PageNotFoundException();
|
||||
}
|
||||
|
||||
Application::$app->controller = new $callback[0]();
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* de adaugat aici middleware urile asociate unui controller
|
||||
*/
|
||||
|
||||
return call_user_func($callback, $this->request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user