Laravel 5.6 additional Route::resource() Parameters

19,433

Resource Controllers must implement a defined set of methods which are then mapped to the appropriate HTTP verb and path by the router. These methods, paths and verbs form part of a contract that cannot be adjusted, otherwise working with a Laravel application that implements Resource Controllers would be a headache.

Resource Controllers excel in providing the same experience across all Laravel applications, if your application requires behaviour that isn't supported out of the box by Resource Controllers then it is a sign that you should not be using them and should instead register your routes manually.

If you have just one route that needs to implement custom behaviour then you can register some methods instead of all and then choose to register a custom route to your Resource Controllers method, something like:

Route::resource('customers', 'CustomerController')->except([
    'index'
]);

Route::get('/customers/{page?}', 'CustomerController@index');

The documentation on Resource Controllers covers except and only.

Share:
19,433
Ricky
Author by

Ricky

Since I 'surfed' the web for the first time, the Internet has become one of my main passions, making me choose a career in this area. My other passions are music, cinema, TV shows, learning, eating white chocolate, cooking 'francesinhas' (mail me if you're curious about that), playing around with Photoshop and Illustrator, programming, social networking and, as an eternal fan of the X-Files, I love a good conspiracy. As a young and healthy man, I love sports and practice as much as I can off-road biking, hiking and jogging. In a professional standpoint, I'm a Front-end Developer, but I like to be known as a World Wide Web Worshipper :) Among working with clients from various business areas and markets, I'm currently working on a personal e/m-Commerce project for the retail and consumer industry.

Updated on June 13, 2022

Comments

  • Ricky
    Ricky over 1 year

    I would like to know how to add additional parameters to Laravel's Route Resource without using Query Strings.

    I created a controller (CustomerController) with all the built-in resources and then, added the following route:

    Route::resource('customers', 'CustomerController');
    

    What i would like to do is add additional parameters to some of the default resources without creating custom routes or using query strings. For example:

    Default resource with optional parameter (index):

    public function index($page = 0)
    {
        //...
    }
    

    Desired URL:

    http://www.example.com/customers
    http://www.example.com/customers/{page}
    

    I tried the following, but i get a not found exception (NotFoundHttpException):

    Route::resource('customers', 'CustomerController')->parameters([
        'index' => 'page'
    ]);
    

    Is this possible? If so, how can i accomplish it?

  • Ricky
    Ricky over 5 years
    Hi @sam, thanks for the detailed explanation. I'm using resources because i really don't need any extra features other than the ones provided by Laravel. In this case i'm going use your approach. Once again thanks :)
  • emmaakachukwu
    emmaakachukwu almost 3 years
    This doesn't necessarily answer the question. This just renames the route parameter in the routes with parameters already. The question seeks to add additional parameter