Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

php - How to use the request route parameter in Laravel 5 form request?

I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.

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.

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.

I can use the id in the controller method like so:

public function destroy($id, DeletePivotRequest $request)
{
    Resource::findOrFail($id);
}

But how to get this to work in the authorize method of the Form Request?

question from:https://stackoverflow.com/questions/30051970/how-to-use-the-request-route-parameter-in-laravel-5-form-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's very simple, just use the route() method. Assuming your route parameter is called id:

public function authorize(){
    $id = $this->route('id');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...