Laravel 向资源控制器添加自定义方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36083062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel add custom method to resource controller
提问by Eliya Cohen
I'm using laravel 5.2 and I wanted to know if there's an option to include into the resource more methods.
我正在使用 laravel 5.2,我想知道是否可以选择将更多方法包含在资源中。
for example, Id'e like to create a POST method called getUsersList
which I can limit the results. I know I can just add in the routes separately from the resource a new route, but I would need to do this for every route I do.
例如,我想创建一个getUsersList
可以限制结果的 POST 方法。我知道我可以在与资源分开的路由中添加一条新路由,但我需要为我所做的每条路由都这样做。
What's the best way to do this?
做到这一点的最佳方法是什么?
回答by Alexey Mezenin
Of course you can add new actions (methods) to RESTful controllers.
当然,您可以向 RESTful 控制器添加新的操作(方法)。
Just add method and create the route for this action:
只需添加方法并为此操作创建路由:
Route::post('foo/bar', 'FooController@bar');
And don't forget to put this route before RESTful route:
并且不要忘记将这条路线放在 RESTful 路线之前:
Route::post('foo/bar', 'FooController@bar');
Route::resource('foo', 'FooController');