Laravel 4.2 non-ambiguous named routes in group - laravel-4

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

Related

Array to string conversion in named route group in laravel 5.6

My named route group is working fine without resource route. But when I am trying to use 'resource route' then getting this error. Would someone help me please, in where I am doing wrong?
My Route Group is -
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'auth:admin'], function () {
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Admin\AdminController#dashboard'));
Route::group(['prefix' => 'student', 'as' => 'student.'], function () {
Route::resource('admission', array('as' => 'admission', 'uses' => 'Admin\StudentController'));
}); });
You need to pass resource controller name as string as the second parameter for Route::resource():
Route::resource('admission', 'Admin\StudentController');
You don't need to specify routes names with 'as' => 'admission' because the Route::resource() will do that automatically.

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.

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 - using route name in another route

I am trying to use named route in another route like this, but it doesnt work.
Route::get('/home', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::post(route('home'), '.....');
You can use route('home', [], false) in this case. Third parameter false here is to use relative path instead of absolute (default set to true):
public function route($name, $parameters = array(), $absolute = true);
Laravel could do this for you by using a RESTful Resource Controller and allow GET and STORE (to match your example with GET and POST ->http://laravel.com/docs/5.0/controllers)
Route::resource('photo', 'PhotoController', ['only' => ['index', 'store']]);
Run php artisan route:list to see the names of the routes. Or a group of routes (http://laravel.com/docs/5.0/routing):
Route::group(['namespace' => 'admin'], function()
{
Route::get('home', ['as' => 'admin.home.index', 'uses' => 'HomeController#index']);
Route::get('castle', ['as' => 'admin.castle', 'uses' => 'HomeController#castle']);
});

Resources