Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel named route for resource controller

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.

like image 624
flyingL123 Avatar asked Aug 13 '14 15:08

flyingL123


People also ask

How do I name a resource route in Laravel?

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.

What is the use of Route resource in Laravel?

Route::resource: The Route::resource method is a RESTful Controller that generates all the basic routes required for an application and can be easily handled using the controller class.

How do you call a page with a controller in Laravel?

run composer dump-autoload, and then you may call this function from anywhere, let it be controller view or model. or if you don't need to put in the helpers. You can just simply call it from it's controller. Just make it a static function .

How to create a resource route in Laravel?

For resource you have to do two things on the laravel application. first, you have to create a resource route on laravel they provide insert, update, view, delete routes and second, you have to create a resource controller that will provide method for insert, update, view, and delete.

What is Laravel 9 controller resource?

Laravel 9 provide a convenient way to create controllers & route with a resource so that we need to develop methods & routes manually for CRUD operations. Let’s dive into it.

What is route controller in Laravel?

These controllers let you create your controller classes using methods that are used for handling various requests. It would be a lot easier if we understand the concept of laravel route controller with the help of an example.

What is the Laravel only() method?

The laravel only () method helps you to show only the required methods. You can use it the same way as except () method: Hurray! we have completed all the steps to learn the Laravel 9 resource controller with resource routes.


2 Answers

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:

  1. 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...     ] ]); 
  2. 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.

like image 117
Aken Roberts Avatar answered Sep 23 '22 09:09

Aken Roberts


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 to route('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);  });  
like image 24
Marc Avatar answered Sep 19 '22 09:09

Marc