rofavadeka
rofavadeka

Reputation: 587

Laravel routing, filter loops

Rewrote my routing and filter script and it worked, so probably made a stupid mistake the first time around. Posted a minified version of the code (removed most of the routes) as answer below. Also accepted the first answer so the question is answered.

Started with discovering the wonders of laravel, but for now i am stuck.

What i did

What i am trying to accomplish

I want a few page's accessible for one group, but not for group two and vice versa. And if a user is from group one, but requests a page from group two he gets redirected to the '/' rout of group one.

My attempt is what resulted in a infinitive loop of redirecting madness

An Example

Let's say John Doe makes an get request to the url. He land's on the '/' route. John is not logged in (or does not met whatever the condition is). So John belongs to group one of people that are not logged in. John get's redirected to the login page.

John is now in the position to login and log's in. Now John belongs to group two, which consists of people that are logged in. He now must be unable to see the login page. So when a person of group two makes an request to a page meant for group one (for example /login) he or she must be redirected to a page meant for group two (for example '/').

$a = true;
if($a){
  Route::get('/', function(){return Redirect::to('login');});
  Route::get('login', function(){View::make();});
}else{
  Route::get('login', function(){return Redirect::to('/');});
  Route::get('/', function(){View::make();});
}

The Question: How do i do this using laravel route's?

Upvotes: 0

Views: 1480

Answers (2)

rofavadeka
rofavadeka

Reputation: 587

Probably made i dumb mistake, rewrote code and now it works:

filters:

Route::filter('access', function() {
    if (Config::get('app.lockapp') && empty(Session::get('test_access'))) {
        return Redirect::to('getaccess');
    }
});

Route::filter('lock', function() {
    if (!Config::get('app.lockapp') || !empty(Session::get('test_access'))) {
        return Redirect::to('/');
    }
});

Routes:

//test enviorment
Route::group(array('before' => 'lock'), function() {

    Route::get('/', 
        'TestController@redirect_test'
    );

    Route::get('test',
        'TestController@show_authentication'
    );

});

//application
Route::group(array('before' => 'access'), function() {

    Route::get('/', 
        'AppController@show_app'
    );

});

Upvotes: 1

Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

Here the group A is not authorized and B is authorized. So you can check login status.eg.

Route::get('login', function()
{
    if(Auth::user()) {
        return Redirect::to('admin');
    }

    return View::make('login');
});

You can use Auth::user() in all A group pages to filter user.

Upvotes: 1

Related Questions