Laravel dingo api group by multiple domains - laravel

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

Related

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

Laravel routing groups

I have a question about routing groups. I have two types of users and I can not use the role system. Following the laracast email verification video I was able to get a new type of user to work. So I can login and register no problem. However when I have both types of users routes going it starts rejecting logins and such.
I even tried separating the admin user routes and putting the artist user routes on a different php document but still will not allow two types of logins or to view the proper dashboard.
I have tried using namespace inside the group, prefix, and tried middleware to no avail.
Here's the routing code.
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('/artist', function () {
return view('artist');
});
Route::get('/sponsor', function () {
return view('sponsor');
});
Route::get('/viewer', function () {
return view('viewer');
});
Route::get('/contact', function () {
return view('contact');
});
// This is for the Artist Linkings
//Route::group(['middleware' => 'artist'], function () {
//Route::auth('artist');
//Route::get('art/dashboard', 'SessionsController#index');
//Route::get('art/dashboard', ['middleware' => 'artist', function() {
//return view('art/dashboard');
//}]);
//});
//Route::get('art/register', 'RegistrationController#register');
//Route::post('art/register', 'RegistrationController#postRegister');
//Route::get('register/confirm/{token}', 'RegistrationController#confirmEmail');
//Route::get('art/login', 'SessionsController#login');
//Route::post('login', 'SessionsController#postLogin');
//Route::get('/logout', 'SessionsController#logout');
//Route::get('art/dashboard', 'SessionsController#index');
//});
// Need to add the password stuff ect
// Route::group(['prefix' => 'viewer', 'namespace' => 'Viewer'], function () {
// require app_path('Http/Routes/viewers.php');
// });
// This is for all the Viewer Linkings
Route::get('viewer/register', 'ViewerRegistrationController#register');
Route::post('viewer/register', 'ViewerRegistrationController#postRegister');
Route::get('viewer/register/confirm/{token}', 'ViewerRegistrationController#confirmEmail');
Route::get('viewer/login', 'ViewerSessionsController#login');
Route::post('login', 'ViewerSessionsController#postLogin');
Route::get('/logout', 'ViewerSessionsController#logout');
Route::get('viewer/dashboard', 'ViewerSessionsController#index');
//});
//}]);
You can use this .
For artist user as you define
Route::group(["middleware" => ["auth.artist"], "prefix" => "artist","namespace"=>"Artist"], function() {
Route::controller('artist', 'UsersArtistController');
Route::controller('controles', 'controlsArtistController');
});
For viewer user if no any auth needed
Route::group("prefix" => "viewer","namespace"=>"Viewer"], function() {
Route::controller('Viewer ', 'UsersViewer Controller');
Route::controller('controles', 'controlsViewerController');
});
Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware'=>['auth','admin']], function (){
Route::get('dashboard','DashboardController#index')->name('dashboard');
Route::resource('tag','TagController');
Route::resource('category','CategoryController');
});
Route::group(['middleware'=>['auth']], function(){
Route::post('favorite/{post}/add','FavoriteController#add')->name('post.favorite');
Route::post('review/{id}/add','ReviewController#review')->name('review');
});
Route::group(['as'=>'user.','prefix'=>'user','namespace'=>'Author','middleware'=>['auth','user']], function (){
Route::get('dashboard','DashboardController#index')->name('dashboard');
Route::resource('post','PostController');
});

Laravel: Sentry Routes and Filter groups

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');
});

Laravel Routing - How to pass a variable to subroutes?

I'm using the group logic to filter the admin section of my website. I have a routing like this:
Route::group(array('before' => 'auth'), function() {
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() {
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
//other routes...
});
How to make $datas available to all routes that are included in my group ?
You can share variables:
View::share('datas', $datas);
return View::make('admin/dashboard');
As you stated that you'd like to include $datas in every route, you can make use of the use keyword:
Route::group(array('before' => 'auth'), function()
{
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() use ($datas)
{
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
});
You can learn about the use keyword here.

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