#Custom FormRequest set url-parameter

19 messages · Page 1 of 1 (latest)

little sundial
#

When instantiating a custom form request, I would like to set an url-parameter. I looked through the documentation and found how query parameters could be set on a request, but nothing about url-parameters. It would look as follows when submitting a regular API request:

https://localhost:8080/{parameter}

How would I set this in the request instance?

The example of the instantiated request can be seen in the image attachment.

agile sandal
#

@little sundial Why are you manually instantiating form requests in the first place? That’s not how you use form requests.

little sundial
#

I knew that this was not how form requests were intended to be used, but this was just for testing purposes. But since it is possible to set request headers etc., I thought it should also be possible to set url/route-parameters.

#

Thanks for the response by the way!

agile sandal
#

Well the problem is, form requests classes aren’t simply instantiated like that.

#

They extend the base Request class.

#

So what is the actual problem you’re trying to solve here?

little sundial
#

I would like to inject a request into a controller method, which uses a route parameter for an id. The request is successfully instantiated and the body is passed and readable, but the route parameter cannot be passed. I tried something like this, but this does not work.

agile sandal
#

You don’t need to manually pass them.

#

Given a route like:

Route::post('/categories/{category}/posts', [ArticlePostController::class, 'store');

The {category} parameter value would be accessible via $this->route('category') inside your form request. If you’re using route–model binding, then the value will be the bound model instance.

little sundial
#

I know, that is how routes parameters are read once set. But I want to inject the route parameter into a manually instantiated request. This would go into the following function:

#

and the request is created like this currently

agile sandal
#

🤦‍♂️

#

You can’t just manually instantiate a form request with arbitrary data like that.

#

Like I say, they extend the base Request class, so need a shit-load of other stuff to be instantiated.

#

Which is why you type-hint them in your controller actions instead.

#

Use them properly. Access route parameters properly.

little sundial
#

I will look into it, thanks for the help anyway!