As organized as possible I think it’s something like this,
route:
- All my routes for the admin panel are inside the admin prefix filter.
- Inside the admin we have two filters.
- admin.guest, that if you try to access any route within this filter you will be redirected to the route named 'admin.home'.
- admin.auth, that if you try to access any route within this filter you will be redirected to the route named 'admin.getLogin''.
Check out how is my code ( I will not document here so the post does not get grid 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');
}
}
});
Obs¹: the ' Admin...' that has in uses in the route is the namespace of my controller, this is another way to organize more code but is not the case now
Note²: the filter 'csrf' is for form authentication in the Laravel, it checks if the form comes with a token or check it, see its filter in the file Filters
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 guess it didn’t get too complicated for you to understand.