Added: Router route groups

This commit is contained in:
Filip Rojek 2024-12-26 17:47:11 +01:00
parent 4c44dac115
commit 85209ff134
3 changed files with 53 additions and 5 deletions

View File

@ -6,4 +6,6 @@
- [ ] edit user data - change password, mail... - [ ] edit user data - change password, mail...
## Core of the app ## Core of the app
- [ ] think about it lol - [ ] Habits list
- [ ] Habits create
- [ ] Habits track

View File

@ -3,6 +3,8 @@
class Router { class Router {
private $routes = []; private $routes = [];
private $middlewares = []; private $middlewares = [];
private $groupPrefix = '';
private $groupMiddlewares = [];
/** /**
* Add a route with a specific action and optional middleware * Add a route with a specific action and optional middleware
@ -12,9 +14,35 @@ class Router {
* @param array $middlewares Optional middlewares for this route * @param array $middlewares Optional middlewares for this route
*/ */
public function add($route, $action, $middlewares = []) { public function add($route, $action, $middlewares = []) {
$route = $this->groupPrefix . $route;
$middlewares = array_merge($this->groupMiddlewares, $middlewares);
$this->routes[$route] = ['action' => $action, 'middlewares' => $middlewares]; $this->routes[$route] = ['action' => $action, 'middlewares' => $middlewares];
} }
/**
* Define a group of routes with shared prefix and middlewares
*
* @param string $prefix
* @param array $middlewares
* @param callable $callback
*/
public function group($prefix, $middlewares, $callback) {
// Save the current state
$previousPrefix = $this->groupPrefix;
$previousMiddlewares = $this->groupMiddlewares;
// Set new group prefix and middlewares
$this->groupPrefix = $previousPrefix . $prefix;
$this->groupMiddlewares = array_merge($this->groupMiddlewares, $middlewares);
// Execute the callback to define routes in the group
$callback($this);
// Restore the previous state
$this->groupPrefix = $previousPrefix;
$this->groupMiddlewares = $previousMiddlewares;
}
/** /**
* Dispatch the current request to the correct route and execute middlewares * Dispatch the current request to the correct route and execute middlewares
*/ */
@ -22,6 +50,11 @@ class Router {
$uri = $_SERVER['REQUEST_URI']; $uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri, PHP_URL_PATH); $uri = parse_url($uri, PHP_URL_PATH);
// Normalize the URI by removing trailing slash (except for root "/")
if ($uri !== '/' && substr($uri, -1) === '/') {
$uri = rtrim($uri, '/');
}
if (array_key_exists($uri, $this->routes)) { if (array_key_exists($uri, $this->routes)) {
$route = $this->routes[$uri]; $route = $this->routes[$uri];
$middlewares = $route['middlewares']; $middlewares = $route['middlewares'];

View File

@ -25,10 +25,23 @@ require_once models . 'User.php';
$router = new Router(); $router = new Router();
$router->add('/', 'HomeController@index'); $router->add('/', 'HomeController@index');
$router->add('/home', 'HomeController@home'); $router->add('/home', 'HomeController@home');
$router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']);
// auth routes // auth routes
$router->add('/auth/signin', 'AuthController@signin'); $router->group('/auth', [], function ($router) {
$router->add('/auth/signup', 'AuthController@signup'); $router->add('/signin', 'AuthController@signin');
$router->add('/auth/logout', 'AuthController@logout'); $router->add('/signup', 'AuthController@signup');
$router->add('/logout', 'AuthController@logout');
});
// dashboard route
$router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']);
// habits routes
$router->group('/habits', ['RequireAuth'], function ($router) {
$router->add('', 'HabitController@index');
$router->add('/create', 'HabitController@create');
$router->add('/edit/{id}', 'HabitController@edit');
$router->add('/delete/{id}', 'HabitController@delete');
});
$router->dispatch(); $router->dispatch();