Laravel: Sentry Routes and Filter groups - laravel

I am trying Sentry for Laravel and I came accross with this problem. I have 2 group of routes:
Route::group(array( 'before' => 'Sentry|inGroup:Administrator'), function(){
Route::get('/', 'HomeController#index');
Route::resource('user', 'UserController');
});
Route::group(array( 'before' => 'Sentry|inGroup:Doctor'), function(){
Route::get('/', 'HomeController#index');
//Route::resource('user', 'UserController');
});
And my filters are:
Route::filter('inGroup', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
$group = Sentry::findGroupByName($value);
//dd($user->getGroups());
//dd($user->inGroup($group));
if( ! $user->inGroup($group)){
return Redirect::to('/login')->withErrors(array(Lang::get('user.noaccess')));
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('group.notfound')));
}
});
Route::filter('hasAccess', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
//dd($user); $user->hasAccess($value)
if( Sentry::check() ) {
//return Redirect::to('/')->withErrors(array(Lang::get('user.noaccess')));
return Redirect::to('/');
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}
});
The problem is the latter route with the 'Sentry|inGroup:Doctor' is firing. The filter is not getting the Administrator part or group.
How can I achieve to filter them based on the parameter that is passed on by the routes? Or, how can I make them dynamic?

You have defined twice the same "/" route, once in the first (Administrator's) group, and once in the second (Doctor's) group
Route::get('/', 'HomeController#index');
Since you haven't specified any prefix on either route group, the later (second) route overrides the first and the Administrator route cannot be reached.
You can try using prefixes:
// http://localhost/admin
Route::group(array('prefix' => 'admin', 'before' => 'Sentry|inGroup:Admins'), function()
{
Route::get('/', 'HomeController#index');
});
// http://localhost/doctors
Route::group(array('prefix' => 'doctors', 'before' => 'Sentry|inGroup:Doctor'), function()
{
Route::get('/', 'HomeController#index');
});

Related

Laravel route group prefix - variable not working

in web.php :
Route::group(['middleware'=>['checklang','checkmoney']],function(){
Route::get('/', function () {
return redirect('/'.session()->get('lang'));
});
Route::group([
'prefix' => '{locale}',
'where'=>['locale'=>'[a-zA-Z]{2}']],
function() {
Route::get('/tour/{id}','HomeController#getTours');
});
});
in HomeContoller :
public function getTours($id){
dd($id);
}
when trying to access url : example.com/en/tour/5
getting result
en , but should be 5
Where is a problem and how to solve it?
Your route has 2 variables, {locale} and {id}, but your Controller method is only referencing one of them. You need to use both:
web.php:
Route::group(['prefix' => '{locale}'], function () {
...
Route::get('/tour/{id}', 'HomeController#getTours');
});
HomeController.php
public function getTours($locale, $id) {
dd($locale, $id); // 'en', 5
}
Note: The order of definition matters; {locale} (en) comes before {id} 5, so make sure you define them in the correct order.

Laravel dingo api group by multiple domains

Is it possible to group dingo api routes with multiple domains? For example
$api->group(['domain' => ['something.com', 'anotherexample.com]], function ($api) {
// My routes
});
You don't have to repeat code.
$callback = function() {};
Route::group(['domain' => 'foo.bar.dev'], $callback);
Route::group(['domain' => 'foo.bar'], $callback);
But we have one more micro solution
Route::macro("domain", function(array $domains, \Closure $definition) {
foreach ($domains as $domain) {
Route::group(['domain' => $domain], $definition);
}
});
Route::domain(['foo.bar.dev', 'foo.bar'], function($route) {
// Do stuff
});

Laravel build multi-lang route system

im using Laravel 5, and i have this route:
Route::group(['prefix' => '{lang}/', 'middleware' => 'SetLanguage'], function($lang){
//element Show
Route::get('/'.trans('routes.element-container').'/{slugName}', 'ElementController#showByName');
});
My middleware is this:
public function handle($request, Closure $next)
{
if (in_array($request->lang, config('app.all_langs'))){
//exit("SETTING ON ".$request->lang);
App::setLocale($request->lang);
}else{
//exit("SETTING ON en");
App::setLocale('en');
}
return $next($request);
}
If i un-comment the two exit it works, but, the function "trans" on route side is not working, seems to trans only in default lang.
why the "trans" function is called before the middleware?
I have test with 'before' and 'after', but no work...
If I understood you correctly, you could consider something like this:
Route::group(['prefix' => '{lang}/', 'middleware' => 'SetLanguage'], function() {
foreach (config('app.all_langs') as $language) {
$translatedRoute = trans('routes.element-container', [], $language);
Route::get("/$translatedRoute/{slugName}", 'ElementController#showByName');
}
});
But this will also register routes for e.g. /de/element-container/element-a.
Alternative:
foreach (config('app.all_langs') as $language) {
Route::group(['prefix' => $language, 'middleware' => 'SetLanguage'], function() {
$translatedRoute = trans('routes.element-container', [], $language);
Route::get("/$translatedRoute/{slugName}", 'ElementController#showByName');
});
}
This will register /en/element-container/element-a and /de/Elemente-Behälter/element-a, but not /de/element-container/element-a

Redirect entire route group to another url?

Given a route group like Route::group(['prefix' => 'foo',]) how would I redirect all routes under this group to domain.com?
I've tried:
Route::group(['prefix' => 'foo'], function() {
Route::get('/', function () {
return Redirect::to('http://domain.com');
});
});
But that would only redirect myoldsite.com/foo to domain.com, anything such as myoldsite.com/foo/deeper/path would just be a 404. Is there a way to catch all sub routes and redirect them?
You can add a catch-all parameter.
Route::group(['prefix' => 'foo'], function() {
Route::get('/{atsl}', function ($atsl) {
return Redirect::to('http://domain.com');
});
});

how to use the laravel subdomain routing function

I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

Resources