Laravel - using route name in another route - laravel

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

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.

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 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