refactor
- move elixForms API logic to specific folder - move everything else under Api folder
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
|
||||
class Router {
|
||||
private $routes = [];
|
||||
|
||||
public function __construct(\Core\Container $container) {
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function register($method, $path, $handler) {
|
||||
$this->routes[$method][$path] = $handler;
|
||||
}
|
||||
|
||||
public function get($path, $handler) { $this->register('GET', $path, $handler); }
|
||||
public function post($path, $handler) { $this->register('POST', $path, $handler); }
|
||||
|
||||
public function dispatch(Request $req, Response $res) {
|
||||
$method = $req->method();
|
||||
$path = $req->path();
|
||||
|
||||
// extract version prefix /v1/...
|
||||
if (preg_match('#^/v([0-9]+)(/.*)?$#', $path, $m)) {
|
||||
$version = $m[1];
|
||||
$pathWithoutVersion = $m[2] ?? '/';
|
||||
} else {
|
||||
$version = '1';
|
||||
$pathWithoutVersion = $path;
|
||||
}
|
||||
|
||||
// try exact route with versioned namespace
|
||||
$routeKey = $pathWithoutVersion;
|
||||
if (!isset($this->routes[$method][$routeKey])) {
|
||||
return $res->json(['error' => 'Not found'], 404);
|
||||
}
|
||||
|
||||
$handler = $this->routes[$method][$routeKey];
|
||||
// handler can be 'UsersController@index' or 'Controllers\\UsersController@index'
|
||||
if (strpos($handler, '@') !== false) {
|
||||
list($class, $function) = explode('@', $handler);
|
||||
// if class not namespaced, prefix with Controllers\V{n}\
|
||||
if (strpos($class, '\\') === false) {
|
||||
$class = "Controllers\\V{$version}\\" . $class;
|
||||
}
|
||||
} else {
|
||||
return $res->json(['error' => 'Invalid handler'], 500);
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
return $res->json(['error' => 'Controller not found'], 500);
|
||||
}
|
||||
|
||||
// snippet inside Router::dispatch
|
||||
$controller = $this->container->make($class);
|
||||
return $controller->$function($req, $res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user