Laravel excluding resourceful functions in a clean way
Asked Answered
E

3

6

Currently I have these 4 resourceful routes in my web.php file.

Route::resource('campaigns', 'CampaignController')->except(['show']);
Route::resource('users', 'UserController')->except(['show']);
Route::resource('models', 'ModelController')->except(['show']);
Route::resource('trims', 'TrimController')->except(['show']);

And I can't help but wonder. Can't I add something to the Route::resources function to make it behave this way? This because they all have one thing in common. They except() the show() method.

It want to have something like this. (This example does not work because resources() does not have an except() method.

Route::resources([
    'campaigns' => 'CampaignController',
    'users' => 'UserController',
    'models' => 'ModelController',
    'trims' => 'TrimController'
])->except(['show']);
Elsie answered 12/2, 2020 at 13:8 Comment(0)
C
9

This question is already pretty old, but i just faced the same problem and solved it this way:

Route::resources([
    'campaigns' => 'CampaignController',
    'users' => 'UserController',
    'models' => 'ModelController',
    'trims' => 'TrimController'
], [
    'except' => ['show']
    // you can set here other options e.g. 'only', 'except', 'names', 'middleware'
]);

According to source code, method resources is not chainable, because it returns void. But you still can pass the options into second argument of resources.

This is "Larawel-way" and you have not overwrite any vendor code.

Dig here to get more info, how it works.


Note that if you want to exclude show method, but try to reach /users/{user} (HTTP GET) in browser, router will throw The GET method is not supported for this route. Supported methods: PUT, PATCH, DELETE. error. It happens because router still has this route, but for PUT, PATCH, and DELETE. Then your application crashes.

So, maybe you want show HTTP 404 if someone accidentally goes to "show" page. I prefer to add fallback route (this should be the last route of your routes!)

Route::fallback(function () {
    abort(404);
});
Church answered 15/2, 2021 at 17:30 Comment(0)
B
5

You can write your own class that extends Illuminate\Routing\Route and implement your resources method like that:

namespace App\Extends;

class Route extends Illuminate\Routing\Route {

   public function resources($routes, array $excepts) {
     foreach ($routes as $key => $value){
         $this->resource($key, $value)->except($excepts);
     }
   }

}

After you will need to bind your class in your service provider like this:

public function register()
{
    $this->app->bind('Illuminate\Routing\Route', 'App\\Extends\\Route');
}

and call resources in web.php like that:

Route::resources([
    'campaigns' => 'CampaignController',
    'users' => 'UserController',
    'models' => 'ModelController',
    'trims' => 'TrimController'
], ['show']);

[EDIT 1]

From laravel.com/docs/5.8/controllers#resource-controllers

You may register many resource controllers at once by passing an array to the resources method:

Route::resources([
    'photos' => 'PhotoController',
    'posts' => 'PostController'
]);

But I don't know if you can call ->except(['show']) like that

Route::resources([])->except(['show'])
Blinding answered 12/2, 2020 at 14:14 Comment(3)
Thanks, but I don't want to rewrite / override a function. My question was basically is this suported by Laravel at the moment. This is a nice solution though.Elsie
That's it I guess :). Thanks although I did not find what I wanted!Elsie
Thanks again for opening the gates of possibility. :)Glauce
G
1

If you talk about possibility then yes it's possible as above answer mentioned but by default you can't

Take a look at this file,

/vendor/laravel/framework/src/Illuminate/Support/Facades/Route.php

 * @method static \Illuminate\Routing\PendingResourceRegistration resource(string $name, string $controller, array $options = [])
 * @method static void resources(array $resources)
Glauce answered 13/2, 2020 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.