user1575921
user1575921

Reputation:

failed to open page to many redirect

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

Answers (2)

itachi
itachi

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()
{
    });
});

update

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

SamV
SamV

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:

  1. Is user logged in? No, redirect to admin/login.
  2. Laravel then checks the loginsession filter and then tries to redirect again.
  3. Go to step 1.

Move the login route outside of the group that has the filter loginsession.

This line:

Route::get('/login', 'AdminDashboardController@getLogin');

Upvotes: 1

Related Questions