Laravel named route for resource controller
PhpLaravelLaravel 4Php Problem Overview
Using Laravel 4.2, is it possible to assign a name to a resource controller route? My route is defined as follows:
Route::resource('faq', 'ProductFaqController');
I tried adding a name option to the route like this:
Route::resource('faq', 'ProductFaqController', array("as"=>"faq"));
However, when I hit the /faq route and place {{ Route::currentRouteName() }}
in my view, it yields faq.faq.index
instead of just faq
.
Php Solutions
Solution 1 - Php
When you use a resource controller route, it automatically generates names for each individual route that it creates. Route::resource()
is basically a helper method that then generates individual routes for you, rather than you needing to define each route manually.
You can view the route names generated by typing php artisan routes
in Laravel 4 or php artisan route:list
in Laravel 5 into your terminal/console. You can also see the types of route names generated on the resource controller docs page (Laravel 4.x | Laravel 5.x).
There are two ways you can modify the route names generated by a resource controller:
-
Supply a
names
array as part of the third parameter$options
array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.Route::resource('faq', 'ProductFaqController', [ 'names' => [ 'index' => 'faq', 'store' => 'faq.new', // etc... ] ]);
-
Specify the
as
option to define a prefix for every route name.Route::resource('faq', 'ProductFaqController', [ 'as' => 'prefix' ]);
This will give you routes such as
prefix.faq.index
,prefix.faq.store
, etc.
Solution 2 - Php
For answer seekers with Laravel 5.5+ finding this page:
Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function () {
Route::resource('users','UserController');
});
These options will result in the following for the Resource:
-
namespace()
sets Controller namespace to \Admin\UserController -
prefix()
sets request URi to /admin/users -
name()
sets route name accessor toroute('admin.users.index')
> In name()
the DOT is intended, it is not a typo.
Please let others know if this works in comments for any versions prior to Laravel 5.5, I will update my answer.
Update:
Taylor accepted my PR to officially document this in 5.5:
https://laravel.com/docs/5.5/routing#route-group-name-prefixes
UPDATE LARAVEL 8
> New in Laravel 8, the need to use a namespace
in route configs is deprecated, the default namespace
wrapper in RouteServiceProvider
has been removed from Laravel's standard config.
This change decouples Controller namespaces from having to be considered when grouping routes, dropping the namespace
requirement when registering routes gives much more freedom when organizing controllers and routes.
With Laravel 8, the original example in the top half of this post would now look as follows using self references to static class name:
use \App\Http\Controllers\Admin\{
UserController,
ProductController,
AnotherController,
}
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
Route::resource('another', AnotherController::class);
});
Solution 3 - Php
I don't know if it's available in laravel 4.2 (I tested in 5.7) but you can use names
to change the name of all routes generated by resource
Route::resource('faq', 'ProductFaqController', ['names' => 'something']);
and the result will be like this
something.index
and you don't need to specify each route
Solution 4 - Php
All Updates Later then Laravel 5.5 Using
Route::resource('faqs', 'FaqController', ['as' => 'faqs']);
if we not use ['as' => 'faqs']
in above code then it will also work same.
[Updated]
Important to keep in mind that this will work for "resource"
For example:
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
and result will be like
POST | admin/posts/tags | posts.tags.store
GET|HEAD | admin/posts/tags | posts.tags.index
GET|HEAD | admin/posts/tags/create | posts.tags.create
DELETE | admin/posts/tags/{tag} | posts.tags.destroy
PUT|PATCH | admin/posts/tags/{tag} | posts.tags.update
GET|HEAD | admin/posts/tags/{tag} | posts.tags.show
GET|HEAD | admin/posts/tags/{tag}/edit | posts.tags.edit
Solution 5 - Php
Tested with Laravel 8:
You can define your name for resource route as passing names as optional arguments. For Example:
use App\Http\Controllers\UsersController;
Route::resource('reservations', UsersController::class, ['names' => 'users']);
The above example defines routes such as users.index
, users.store
etc.
You can also pass the route names as:
Route::resource('reservations', UsersController::class, ['names' => 'admin.users']);
which will define routes with prefix of admin
such as admin.users.index
, admin.users.store
Solution 6 - Php
Using Laravel 5.5
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
important to keep in mind the "resource"
For example, I send something from my project:
Route::resource('admin/posts/tags', 'PostTagController', ['as' => 'posts']);
Solution 7 - Php
And In Laravel 8
Route::resource('product',
App\Http\Controllers\API\Admin\ProductController::class, [
'names' => [
'index' => 'admin.product.index',
'store' => 'admin.product.store',
'update' => 'admin.product.update',
'destroy' => 'admin.product.delete'
]
])->except(['edit', 'create']);
Solution 8 - Php
you dont need to set name in resource in laravel 5.7 that i have test it. it auto generate route name from url.
Solution 9 - Php
Got the same error as you. for me it worked by adding the whole path that is namespace/ControllerName
Route::resource('staffs', 'App\Http\Controllers\StaffController');
Solution 10 - Php
Route::resource('articles','ArticleController', ['names' => 'xyz'])
Solution 11 - Php
You can rename your resource routes
in AppServiceProvider.php
like so:
public function boot()
{
Route::resourceVerbs([
'create' => 'neu',
'edit' => 'bearbeiten',
]);
}
I believe this feature is designed for localization
.