Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fat Free Framework 3.5 conditional routes in .ini format

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?

like image 814
Seether Avatar asked Dec 28 '25 18:12

Seether


1 Answers

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.

like image 107
Daniel Avatar answered Dec 30 '25 07:12

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!