Using route groups
When creating a web app, we may find a few routes that need the same URL prefix or filter. Using Laravel's route groups, we can easily apply these to multiple routes.
Getting ready
For this recipe, we just need a standard installation of Laravel.
How to do it…
To complete this recipe, follow these steps:
In our
app/filters.php
file, create a filter to check for a user:Route::filter('checkUser', function() { if ('user' !== Session::get('profile')) { return 'You are not Logged In. Go Away!'; } });
In the
app/routes.php
file, create a route that can set our profile session:Route::get('set-profile', function() { Session::set('profile', 'user'); return Redirect::to('profile/user'); });
In
routes.php
, create our route group:Route::group(array('before' => 'checkUser', 'prefix' => 'profile'), function() { Route::get('user', function() { return 'I am logged in! This is my user profile.'; }); Route::get('friends', function() { return...