Reputation: 587
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
First i created a few route's.
This worked.
Then i created a filter for a rout group.
This kinda worked, when i placed the rout group behind my default route's. But i could wrap my head around this since the code is executed from the top and gets rewritten when i place it before my default route's by my default route's. Sounds logic.
Multiple group's.
This is where it wen't terrible wrong. I first created two filter's becouse i could not figure out if it was possible to make negative filters or something in that fashion.
One filter was the exact oposite of the other. So if the condition in filter one was true, the condition in filter two was false. Doing this in the hope to accomplish that one rout was only executed when the condition of that rout was met and the other could only be executed when it was not. This resulted in an loop and this i don't understand.
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
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
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