php 如何在 Laravel 5 表单请求中使用请求路由参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30051970/
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
How to use the request route parameter in Laravel 5 form request?
提问by Rohan
I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.
我是 Laravel 5 的新手,我正在尝试使用新的表单请求来验证我的应用程序中的所有表单。
Now I am stuck at a point where I need to DELETE a resource and I created a DeleteResourceRequest for just to use the authorize method.
现在我被困在需要删除资源的地方,我创建了一个 DeleteResourceRequest 来使用授权方法。
The problem is that I need to find what id is being requested in the route parameter but I cannot see how to get that in to the authorize method.
问题是我需要找到路由参数中请求的 id ,但我看不到如何将其输入到授权方法中。
I can use the id in the controller method like so:
我可以像这样在控制器方法中使用 id:
public function destroy($id, DeletePivotRequest $request)
{
Resource::findOrFail($id);
}
But how to get this to work in the authorize method of the Form Request?
但是如何让它在表单请求的授权方法中工作呢?
回答by lukasgeiter
That's very simple, just use the route()
method. Assuming your route parameter is called id
:
很简单,使用route()
方法就行了。假设您的路由参数被称为id
:
public function authorize(){
$id = $this->route('id');
}
回答by Emeka Mbah
You can accessing a Route parameter Value via Illuminate\Http\Request instance
您可以通过 Illuminate\Http\Request 实例访问路由参数值
public function destroy($id, DeletePivotRequest $request)
{
if ($request->route('id'))
{
//
}
Resource::findOrFail($id);
}
回答by William Turrell
Laravel 5.2, from within a controller:
Laravel 5.2,从控制器中:
use Route;
...
Route::current()->getParameter('id');
I've found this useful if you want to use the same controller method for more than one route with more than one URL parameter, and perhaps all parameters aren't always present or may appear in a different order...
如果您想对具有多个 URL 参数的多个路由使用相同的控制器方法,我发现这很有用,并且可能所有参数并不总是存在或可能以不同的顺序出现...
i.e. getParameter('id')
will give you the correct answer, regardless of {id}
's position in the URL.
iegetParameter('id')
会给你正确的答案,不管{id}
's 在 URL 中的位置。
回答by Emmanuel Opio
回答by Bryan
I came here looking for an answer and kind of found it in the comments, so wanted to clarify for others using a resource route trying to use this in a form request
我来这里是为了寻找答案,并在评论中找到了它,所以想为其他人使用资源路由尝试在表单请求中使用它进行澄清
as mentioned by lukas in his comment:
Given a resource controller Route::resource('post', ...)
the parameter you can use will be named post
正如卢卡斯在他的评论中提到的:给定一个资源控制器Route::resource('post', ...)
,你可以使用的参数将被命名post
This was usefull to me but not quite complete. It appears that the parameter will be the singular version of the last part of the resource stub.
这对我很有用,但还不够完整。该参数似乎是资源存根最后一部分的单数版本。
In my case, the route was defined as $router->resource('inventory/manufacturers', 'API\Inventory\ManufacturersController');
就我而言,路线被定义为 $router->resource('inventory/manufacturers', 'API\Inventory\ManufacturersController');
And the parameter available was manufacturer
(the singular version of the last part of the stub inventory/manufacturers
)
可用的参数是manufacturer
(存根最后一部分的单数版本inventory/manufacturers
)