Laravel Subdomain routing, routes undefined inside Route Domain Group - laravel

I am trying to move all my routes inside a subdomain routes group. When I put routes inside subdomain group, issues arise. Routes become undefined. This way works.
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
This way does not work. Links generated with route helper generates undefined errror, href="{{ route('profile.edit') }}"
Route::group(['middleware' => 'auth'], function () {
Route::domain('{username}.'.env('SESSION_DOMAIN'))->group(function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
});
I thought I came up with a solution when I used url helper, href="{{ url('profile.edit') }}" , but a new problem started. Links to other pages were fine, but form submission actions generated a error
Missing required parameters
What am I doing wrong?

your route requires the subdomain parameter. {username} needs to be passed with your route but you are not passing anything. thus the missing required parameters error comes. you have to call a route like
href="{{ route('profile.edit', 'subdomain_name') }}"
or if you have a fixed subdomain then just remove {username} from your route definition and use the exact sub domain.
Route::group(['middleware' => 'auth'], function () {
Route::domain('subdomain_name.'.env('SESSION_DOMAIN'))->group(function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
});

Related

Laravel multi-nested same route parameters

I creating internet-marketn on Laravel 5.2
Need to do something like a breadcrumps in url with categories slug,
like so
/catalog/{category_1}/{category_2}/{category_n}
but get only last parameter and bind it into Model
moreover last category can has product slug and route can be like
/catalog/{category_1}/{category_2}/{category_n}/product/{product_slug}
my routing
Route::group(['prefix' => 'catalog'], function () {
Route::get('/', ['as' => 'shop_catalog', 'uses' => 'CatalogController#catalog']);
Route::group(['prefix' => '{category}'], function () {
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController#category']);
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController#product']);
});
Try write pattern in RouteServiceProvider like
$route->pattern('category', '.*');
Then explode by '/' and get last element for binding. But can't get product parameter.
How can I do this logic?
The ordering of your routes is really important.
As the shop_show_category route is listed first, the router never gets to check if the url more specifically matches your shop_show_product route.
Route::group(['prefix' => '{category}'], function () {
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController#product']);
});
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController#category']);
});
By rearranging so the more specific route is first, you know the router will check the shop_show_product route for a match first. Failing it seeing product/xxx at the end of the url it will try to match the shop_show_category route.

Laravel admin/user route prefix approach

so I have to make this system for transport management. The user can log in create/update/edit all his trips. But the admin can do the same for all users. I have divided user and admin in to route prefixes:
Route::group(['prefix' => 'admin/', 'middleware' => ['auth','admin']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));}
Route::group(['prefix' => 'user/', 'middleware' => ['auth', 'user']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));
}
The problem is in every method of the TripController I have to pass route variable with the correct url (the admin request will have a 'admin' prefix, and the users will have 'user' prefix)
return View('trips.index', compact('users', 'route'));
The question is there a way to do this nicely or should I just pull the trips Route::resource out of the both groups so that it wouldn't have any groups? What is the correct approach here?
I use this approach:
Route::group(['namespace' => 'Admin', 'as' => 'admin::', 'prefix' => 'admin'], function() {
// For Other middlewares
Route::group(['middleware' => 'IsNotAuthenticated'], function(){
// "admin::login"
// http://localhost:8000/admin/login
Route::get('login', ['as' => 'login', 'uses' => 'AdminController#index']);
});
// For admin middlewares
Route::group(['middleware' => 'admin'], function(){
// "admin::admin.area.index"
// http://localhost:8000/admin/area/{area}
Route::resource('Area', 'AreaController');
// "admin::dashboard"
// http://localhost:8000/admin/
Route::get('/', ['as' => 'dashboard', 'uses' => 'AdminController#dashboard']);
});
});
Whenever I need to access url in blade templates I simply use route helper method.
// For resourceful routes
{{ route('admin::admin.city.index') }}
or
//For regular get/post routes
{{ route('admin::dashboard') }}
Or simply run artisan command to list route names.
php artisan route:list
I did it with this:
//Admin Group&NameSpace
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard')->middleware('auth');
});
Even you can customize the ->middleware('auth'); with a custom middleware role based.

Resource route within a group route

I am trying to group all the routes for our admin section to access model resources. So far I've come with this:
Route::group(['middleware' => 'auth', 'prefix' => 'admin', 'as' => 'admin::'], function() {
Route::get('dashboard', ['as' => 'dashboard', function() {
return view('pages.dashboard');
}]);
Route::resource('user', 'UserController', ['as' => 'user']);
Route::resource('plan', 'PlanController', ['as' => 'plan']);
Route::resource('answer', 'AnswerController', ['as' => 'answer']);
Route::resource('question', 'QuestionController', ['as' => 'question']);
Route::resource('retailer', 'RetailerController', ['as' => 'retailer']);
Route::resource('restriction', 'RestrictionController', ['as' => 'restriction']);
});
I want to name these routes to access them in a much easier manner by calling their names. However it breaks and says "Route [admin::user] not defined." I want to use the route naming feature to use route('admin::user'). I am having problem with the resource routes. The dashboard one works fine - route('admin::dashboard')
I take from this post that naming resource routes should work (Laravel named route for resource controller)
Resources are given route names automatically run php artisan route:list to list the routes out:
Route::group(['middleware' => 'auth', 'prefix' => 'admin', 'as' => 'admin::'], function() {
Route::get('dashboard', ['as' => 'dashboard', function() {
return view('pages.dashboard');
}]);
Route::resource('user', 'UserController');
});
Resulting Routes
admin::dashboard
admin::admin.user.store
admin::admin.user.index
admin::admin.user.create
admin::admin.user.destroy
admin::admin.user.show
admin::admin.user.update
admin::admin.user.edit

Laravel 5 advance routing with reserved route keywords

Would like to check say that I have the following routes
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile'));
Route::get('/settings/password', array('as' => 'chgPassword', 'uses' => 'ProfileController#updatePassword'));
Route::post('/settings/password', array('as' => 'postChgPassword', 'uses' => 'ProfileController#postUpdatePassword'));
Route::get('/settings/email/request', array('as' => 'chgEmailRequest', 'uses' => 'ProfileController#updateEmailRequest'));
Route::post('/settings/email/request', array('as' => 'postChgEmailRequest', 'uses' => 'ProfileController#postUpdateEmailRequest'));
Route::get('/logout', array('as' => 'logout', 'uses' => 'ProfileController#logout'));
});
Notice that my first route accepts an optional parameter which will then route the user to a specific profile which it works fine, but when ever i have other routes say that /logout, laravel router will also use the /{profile_url?} route instead of the expected logout route. Is there any way that i can specified something like a reserved keyword like
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile')
->except('settings', 'logout'));
something like that? Ho that someone can enlighten me with this issue.
Because you put a wildcard {profile_url?} at the first place, Laravel will ignore the rest. So be careful when using wildcard routes. you should put the least specific route in the last place, Lavarel will check all of specific routes. If it doesn't match, it will go to the wildcard route. For example :
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}',...); // Lavarel do this
Route::get('/logout',...); // ignore this
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/logout',...); // do this if it matches
Route::get('/{profile_url?}',...); // else do this
});

Laravel 4.2 non-ambiguous named routes in group

I have the following code in routes.php:
Route::group(['prefix' => 'dev/order'], function() {
Route::get('create', ['as' => 'dev.order.create', 'uses' => 'OrderController#create']);
Route::get('create-pack', ['as' => 'dev.order.create-pack', 'uses' => 'OrderController#createPack']);
}
);
How can I get rid of duplicate action name, 'dev.order' and 'OrderController' substrings in parameters? Route::controller() and Route::resource() creates unnamed routes when viewed via
php artisan routes
While I need a group of named routes to one controller with common prefix.
Unfortunatelly, there is no route name prefix yet in Laravel, so, to remove repetition of strings, you can do things like this:
$prefix = 'dev.order.';
$controller = 'OrderController#';
Route::group(['prefix' => 'dev/order'], function() use ($prefix, $prefix)
{
Route::get('create', ['as' => $prefix.'create', 'uses' => $controller.'create']);
Route::get('create-pack', ['as' => $prefix.'create-pack', 'uses' => $controller.'createPack']);
});

Resources