Added: Router route groups
This commit is contained in:
parent
4c44dac115
commit
85209ff134
4
TODO.md
4
TODO.md
@ -6,4 +6,6 @@
|
||||
- [ ] edit user data - change password, mail...
|
||||
|
||||
## Core of the app
|
||||
- [ ] think about it lol
|
||||
- [ ] Habits list
|
||||
- [ ] Habits create
|
||||
- [ ] Habits track
|
||||
|
@ -3,6 +3,8 @@
|
||||
class Router {
|
||||
private $routes = [];
|
||||
private $middlewares = [];
|
||||
private $groupPrefix = '';
|
||||
private $groupMiddlewares = [];
|
||||
|
||||
/**
|
||||
* Add a route with a specific action and optional middleware
|
||||
@ -12,9 +14,35 @@ class Router {
|
||||
* @param array $middlewares Optional middlewares for this route
|
||||
*/
|
||||
public function add($route, $action, $middlewares = []) {
|
||||
$route = $this->groupPrefix . $route;
|
||||
$middlewares = array_merge($this->groupMiddlewares, $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
|
||||
*/
|
||||
@ -22,6 +50,11 @@ class Router {
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
$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)) {
|
||||
$route = $this->routes[$uri];
|
||||
$middlewares = $route['middlewares'];
|
||||
|
@ -25,10 +25,23 @@ require_once models . 'User.php';
|
||||
$router = new Router();
|
||||
$router->add('/', 'HomeController@index');
|
||||
$router->add('/home', 'HomeController@home');
|
||||
$router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']);
|
||||
|
||||
// auth routes
|
||||
$router->add('/auth/signin', 'AuthController@signin');
|
||||
$router->add('/auth/signup', 'AuthController@signup');
|
||||
$router->add('/auth/logout', 'AuthController@logout');
|
||||
$router->group('/auth', [], function ($router) {
|
||||
$router->add('/signin', 'AuthController@signin');
|
||||
$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();
|
||||
|
Loading…
Reference in New Issue
Block a user