Laravel named route for resource controller
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.
Laravel Resource Routes Naming Prefix
Route::resource('p/contacts', 'BaseData\PrivateContactsController',["as"=>"private"]);
Route::resource('b/contacts', 'BaseData\ContactController',["as"=>"normal"]);
this way the urls will stay the same, but the names will have a prefix, for the first resource controller
private.contacts.index or private.contacts.edit
and for the second controller
normal.contacts.create or normal.contacts.show
for more info check the documentation or this github issue
Laravel custom action names and route names using a resource controller?
Here is a good solution suggested by Laravel doc:
Route::resource('resource', 'ResourceController', ['names' => [
'index' => 'resource.browse',
'delete' => 'resource.delete',
]]);
The rest will have the default names.
Related section in Laravel docs: https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes
How to change name of Nested Resource Route in Laravel?
The following should work:
Route::resource('photos.captions', 'Photos\PhotoController')
->parameters(['photos' => 'photo_id', 'captions' => 'caption_id']);
The resource names and the parameters names have to match:
- resource:
photos
, parameter:photos
- resource:
captions
, parameter:captions
From the docs:
By default, Route::resource will create the route parameters for your
resource routes based on the "singularized" version of the resource
name. You can easily override this on a per resource basis by using
the parameters method. The array passed into the parameters method
should be an associative array of resource names and parameter names:
Route::resource('users', 'AdminUserController')->parameters([
'users' => 'admin_user'
]);
The example above generates the following URIs for the resource's show
route:
/users/{admin_user}
Laravel - Route::resource vs Route::controller
RESTful Resource controller
A RESTful resource controller sets up some default routes for you and even names them.
Route::resource('users', 'UsersController');
Gives you these named routes:
Verb Path Action Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT|PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy
And you would set up your controller something like this (actions = methods)
class UsersController extends BaseController {
public function index() {}
public function show($id) {}
public function store() {}
}
You can also choose what actions are included or excluded like this:
Route::resource('users', 'UsersController', [
'only' => ['index', 'show']
]);
Route::resource('monkeys', 'MonkeysController', [
'except' => ['edit', 'create']
]);
API Resource controller
Laravel 5.5 added another method for dealing with routes for resource controllers. API Resource Controller acts exactly like shown above, but does not register create
and edit
routes. It is meant to be used for ease of mapping routes used in RESTful APIs - where you typically do not have any kind of data located in create
nor edit
methods.
Route::apiResource('users', 'UsersController');
RESTful Resource Controller documentation
Implicit controller
An Implicit controller is more flexible. You get routed to your controller methods based on the HTTP request type and name. However, you don't have route names defined for you and it will catch all subfolders for the same route.
Route::controller('users', 'UserController');
Would lead you to set up the controller with a sort of RESTful naming scheme:
class UserController extends BaseController {
public function getIndex()
{
// GET request to index
}
public function getShow($id)
{
// get request to 'users/show/{id}'
}
public function postStore()
{
// POST request to 'users/store'
}
}
Implicit Controller documentation
It is good practice to use what you need, as per your preference. I personally don't like the Implicit controllers, because they can be messy, don't provide names and can be confusing when using php artisan routes
. I typically use RESTful Resource controllers in combination with explicit routes.
Related Topics
Generating Random Numbers from Skewed Normal Distribution
PHP How to Write Data to Middle of File Without Rewriting File
Change Single Variable Value in Querystring
Cannot Send Session Cookie - Headers Already Sent
Display Custom Order Meta Data Value in Email Notifications Woocommerce
How to Implode Array with Key and Value Without Foreach in PHP
PHP Cli Getting Input from User and Then Dumping into Variable Possible
How to Enable PHP to Work with Postgresql
Setting a PHP Script as a Windows Service
Remove Duplicate Items from an Array
Understanding Imagemagick's Convert and Translating to Ruby Rmagick
PHP to Clean-Up Pasted Microsoft Input
How to Run Multiple Insert Query in SQL Using PHP in One Go
How to Escape Single-Quote (Apostrophe) in String Using PHP