Restricting access to certain pages
In this recipe, we'll explore how to restrict access to various pages in our app. This way, we can make pages viewable to only those with the correct credentials.
Getting ready
We will be using the code created in the Setting up and configuring the Auth library and Creating an authentication system recipes as the basis for this recipe.
How to do it...
To complete this recipe, follow these steps:
Create a filter in our
filters.php
file that checks for logged-in users. The default Laravelauth
filter will be fine:Route::filter('auth', function() { if (Auth::guest()) return Redirect::guest('login'); });
Create a filter in
filter.php
for checking if a user is an admin:Route::filter('auth_admin', function() { if (Auth::guest()) return Redirect::guest('login'); if (Auth::user()->admin != TRUE) return Redirect::to('restricted'); });
Make a route that we restrict to logged-in users:
Route::get('restricted', array('before' => 'auth', function...