Laravel named route for resource controller
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Assign Names to Resource Controller Routes in Laravel 4.2 for Easy URL Generation
Body:
Laravel's resource controllers offer an easy way to generate routes for RESTful resources, allowing you to handle common CRUD operations by naming a single route and leveraging controllers. However, it is essential to understand how to manage these named routes effectively. In this blog post, we will explore the correct method of assigning names to resource controller routes in Laravel 4.2 for efficient URL generation.
To begin, let's take a look at a standard route definition for a resource controller:
Route::resource('faq', 'ProductFaqController');
In this example, the first parameter is the name of your resource (faq), and the second parameter specifies the corresponding class responsible for handling your routes. This will generate routes for all actions, which are prefixed with the route name followed by the appropriate action suffix:
- index: List all data
- show: Display a single item
- create: Render a form to add an item
- store: Handle form submission to add new items
- edit: Show a form to update an item
- update: Handle form submission to save changes on an item
- destroy: Delete a specific item
Now, let's try assigning custom names to your routes. You may have tried this approach:
Route::resource('faq', 'ProductFaqController', array("as"=>"faq"));
This code snippet seems logical and achieves the intended purpose of assigning a name to your resource controller route. However, there is an essential difference between this implementation and the correct one: you have specified "as" twice. In Laravel 4.2's documentation for Route::resource(), it states that adding `array("as"=>"faq")` will result in naming all routes as 'faq', which is not our objective here.
The proper way to assign a name to your resource controller route is by specifying the named route after all actions are defined:
Route::resource('faq', 'ProductFaqController');
Route::get('{uri}', function($name) { return Route::currentRouteName().'.'.Request::url(); });
Route::pattern('slug', '[^/]+');
$routes = [
'faq.index'=>['as'=>'faq','uses'=>'ProductFaqController@index','before'=>'auth'],
'faq.create'=>['as'=>'faq.create','uses'=>'ProductFaqController@create','before'=>'auth'],
'faq.store'=>['as'=>'faq.store','uses'=>'ProductFaqController@store','before'=>'auth'],
'faq.show'=>['as'=>'faq.show','uses'=>'ProductFaqController@show','before'=>'auth'],
'faq.edit'=>['as'=>'faq.edit','uses'=>'ProductFaqController@edit','before'=>'auth'],
'faq.update'=>['as'=>'faq.update','uses'=>'ProductFaqController@update','before'=>'auth'],
'faq.destroy'=>['as'=>'faq.destroy','uses'=>'ProductFaqController@destroy','before'=>'auth']
];
foreach($routes as $key=>$route) {
Route::get('{slug}', function() use ($name,$route) { return route($route['as']).'.'.$name; });
}
The code above will result in having individual named routes for each action and the main resource. The first block of your code remains the same, while you now explicitly define the named routes for each action (index, create, store, etc.). To achieve this, we have combined custom route definitions with Laravel's resource controller mechanism.
The second snippet creates an additional route handler that takes the current route name and combines it with the given URL. This makes sure that each individual named route maintains its integrity without being affected by any other routes handling the same action.
By following these steps, you will have successfully assigned custom names to your resource controller routes in Laravel 4.2, ensuring efficient URL generation and maintaining a clean structure for your application.