I've defined my routes.ini file with several entries like this:
[routes]
GET /=PublicController->home
GET /login=PublicController->login
GET /logout=PublicController->logout
POST /auth=PublicController->auth
[...]
I was wondering if it's possibile to dynamically modifiy routes depending on arbitrary conditions to check, e.g. session variables and so on.
Is there a way to do something like:
if ($f3->get('SESSION.user.level') == 'admin') {
GET /=AdminController->home
} else {
GET /=UserController->home
}
within an .ini file?
I've been using a slightly different approach, and thought I'd share it as-well.
In my main index.php file I look for the url component, and load the routes (and overload other config setting) according to that
if(explode('/',$_SERVER['REQUEST_URI'])[1] === "admin"){
$f3->config('../f3/app/config/config.admin.ini');
$f3->config('../f3/app/config/routes.admin.ini');
}
I use this instead of the Session, because the controllers used in the router can handle redirection to the login page. You also can continue using routes in ini file, which I prefer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With