Laravel auth check for all pages
Here is my solution.
/**
* Groups of routes that needs authentication to access.
*/
Route::group(array('before' => 'auth'), function()
{
Route::get('user/logout', array(
'uses' => 'UserController@doLogout',
));
Route::get('/', function() {
return Redirect::to('dashboard');
});
Route::get('dashboard', array(
'uses' => 'DashboardController@showIndex',
));
// More Routes
});
// Here Routes that don't need Auth.
There are several ways of applying filters for many routes.
Putting rotues into Route::group()
or if you using controllers add the filter there, add it in the Base_Controller
so it will be applied to all. You can also use filter patterns and use a regex which applies the filter to all except a few you don't want to.
Documentation
Route filters: http://laravel.com/docs/routing#route-filters
Example to the pattern filter, as the others are basicly in the docs. This one could be the fastest but also the most problematic because of the problematic way of registering a regex in this function (the *
is actually converted into (.*)
).
Route::filter('pattern: ^(?!login)*', 'auth');
This will apply auth to any route except example.com/login
.
Route::group(['middleware' => ['auth']], function()
{
Route::get('list', 'EventsController@index');
});
Read more on the documentation page: https://laravel.com/docs/5.2/routing#route-groups