judda's avatar
Level 15

Conditional Routing

Hey all,

I want to add in a "preview" version of my site if you are visiting the site as a guest. However, I don't want to change my route names (if possible).

Short of putting conditional logic about what I should be showing in my controllers, is there anything that I can do to basically say (in the routes):

if guest
    then point to the guest controller
else
    point to the user's controller

I tried to override the routes but it did just that, overrode them, taking the latest one available in my routes.php file. I tried to add an if(Auth::check()) to my routes, but since routes are user-agnostic (and cached), it only takes the logged out version.

Do you guys have any suggestions on how I could do this nicely?

Thanks!

0 likes
4 replies
judda's avatar
Level 15

Sadly route filters won't work, they were my first choice. Because if you name a route one thing, the next one that comes in will override the value.

So, if you have:

Route::get('user', array('as' => 'foo', 'uses' => 'UserController@showProfile', 'before' => 'auth'));
Route::get('user', array('as' => 'foo', 'uses' => 'UserController@showProfile'));

They would be redirected to the 'non-auth' version of it.

bestmomo's avatar

Maybe you could test it in your regular controller (in constructor if there are many methods) and redirect for guest.

judda's avatar
judda
OP
Best Answer
Level 15

Right now, what I ended up doing (which I'm not a fan of) is in the 'before' filter for the user-specific, I have added in 'preview' filter which will basically redirect the user to the same route name but prefixed with 'preview'.

I wanted to avoid having that logic in my controller if possible - so that's why I'm avoiding that one @bestmomo.

Please or to participate in this conversation.