Reputation:
I have problem use below code to build a login system, it failed to open page because too many redirect, I trying to make folder path like
admin/login
admin/
to AdminArticleController.php crud
and any path under admin
should be detect logged in or not, how to fix it?
route.php
Route::group(array('prefix' => 'admin', 'before' => 'loginsession'), function()
{
Route::get('/', function()
{
print 'article';
});
Route::get('/login', 'AdminDashboardController@getLogin');
Route::post('/login', function()
{
});
Route::post('/logout', function()
{
});
});
Route::filter('loginsession', function()
{
if(!Session::has('loggedin'))
{
return Redirect::to('admin/login');
}
});
UPDATE
How to check session exist when visit any file under admin/
include admin/login
or admin/article/..
Route::resource('admin/article','AdminArticleController');
Upvotes: 1
Views: 3389
Reputation: 6393
Route::group(array('prefix' => 'admin', 'before' => 'loginsession'), function()
{
Route::get('/', function()
{
print 'article';
});
Route::get('/login', 'AdminDashboardController@getLogin');
Route::post('/login', function()
{
});
Route::post('/logout', function()
{
});
});
think.... the filter will be applied. but if admin/login
is executed, there is no way he can login because the filter will redirect to the same action.
put login
out of the group . like :
Route::get('/login', 'AdminDashboardController@getLogin');
Route::group(array('prefix' => 'admin', 'before' => 'loginsession'), function()
{
Route::get('/', function()
{
print 'article';
});
Route::post('/login', function()
{
});
Route::post('/logout', function()
{
});
});
As far your update, the filter wont be applied in Resoruce routes.
the easiest way will be
Route::when('admin/*', 'loginsession');
put this anywhere in your route file and done.
Upvotes: 1
Reputation: 7586
You are creating a redirect loop because the login
route is inside the filter that requires you to be logged in. If you are not logged in Laravel will go:
admin/login
.loginsession
filter and then tries to redirect again.Move the login
route outside of the group that has the filter loginsession
.
This line:
Route::get('/login', 'AdminDashboardController@getLogin');
Upvotes: 1