As organized as possible, I think it's something like this,
route:
- All my routes referring to the admin panel are inside the filter with admin prefix.
- Inside admin we have two filters.
- admin.guest, that if you attempt to access any route within this filter you will be redirected to the route named 'admin.home'.
- admin.auth, that if you attempt to access any route within this filter you will be redirected to the route named 'admin.getLogin'.
Check out how my code is (I will not document here for the post not getting gritty and tiring).
filters.php
Route::filter('admin.guest', function()
{
if (Auth::check()) return Redirect::route('admin.home');
});
Route::filter('admin.auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::route('admin.getLogin');
}
}
});
Obs1: the '\ Admin ...' that has in uses in the route is the namespace of my controller, this is another way to organize more the code but it is not the case now
Note: the 'csrf' filter is for form authentication in laravel, it checks if the form comes with a token or checks it, see its filter in the filters file
routes.php
/*
|--------------------------------------------------------------------------
| Panel Admin
|--------------------------------------------------------------------------
*/
Route::group(['prefix' => 'admin'], function(){
Route::group(['before' => 'admin.guest'], function(){
Route::get('login', [
'as' => 'admin.getLogin',
'uses' => '\Admin\UsersController@getLogin'
]);
Route::group(['before' => 'crsf'], function(){
Route::post('login', [
'as' => 'admin.postLogin',
'uses' => '\Admin\UsersController@postLogin'
]);
});
});
Route::group(['before' => 'admin.auth'], function(){
Route::get('/', [
'as' => 'admin.home',
'uses' => '\Admin\HomeController@index'
]);
});
});
I do not think it was too complicated for you to understand.