简体   繁体   中英

In Laravel, passing a file in a custom http request

I'm trying to test a controller method that accepts a file in its request.

In my test I have the following line:

$this->call('POST', 'image/upload', ['image' => $image]);

Where $image is a Symfony\\Component\\HttpFoundation\\File\\UploadedFile instance.

In the controller method I'm testing it accepts the request object and retrieves the uploaded file via:

$image = $request->file('image');

However the file is only available via $request->get('image'); when the action is called via my custom http request above. (An ordinary request from the browser produces the expected behaviour).

How can I pass the file into the file part of the request object?

You are passing the file in as part of the $_POST global where you should be setting it in the $_FILE global.

To do that, I believe you pass the file in as the 5th parameter.

$this->call('POST', 'image/upload', [], [], ['image' => $image]);

Then you should be able to grab it using $request->file('image')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM