Have you tried app()->call([$controller, 'show'], ['download' => 1])?
PHP Tinker Display Controller Methods show's data
I wanted to invoke show in the DownloadsController to load the only added downloadable file. So I added a new controller instance and then tried calling the method and id 1. I however seem to not be able to show the downloadable pdf this way
>>> $controller = app()->make('App\Http\Controllers\Admin\DownloadsController');
=> App\Http\Controllers\Admin\DownloadsController {#5043}
>>> app()->call([$controller, 'show'], ['id' => 1]);
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException with message 'The file "/Users/jasper/code/domain.com/valet/storage/app/" does not exist'
The actual method is this
public function show(Download $download)
{
$extension = pathinfo(storage_path("app/{$download->path}"), PATHINFO_EXTENSION);
return response()->download(storage_path("app/{$download->path}"), "$download->name.{$extension}", [
'Content-Type' => $download->mime,
]);
}
What am I missing here?
That is not to say it won’t work.
The reason for that error is that you have told your show method that it should expect a Download model instance and instead you gave it an integer of 1. So change to this
app()->call([$controller, 'show'], ['download' => Download::first()])
Where you give the Download::first() instead of 1
Let's look at this as a whole to try get a deeper understanding of this
We have a controller with a method called show()
public function show()
{}
That method accepts a parameter that we have used dependancy injection to link to a Download model instance
public function show(Download $download)
{}
Now we want to get the download data
public function show(Download $download)
{
echo $download;
}
If we called this function like this
show(1)
We will get an error as 1 is not an instance of Download
But if we did this
$download = Download::first();
show($download);
We will get no error as we have provided a Download model instance
So, now we can call the controller from the container
app()->call([__CONTROLLER__, __METHOD__], __PARAMS__)
And in this case
use App\Http\Controllers\Admin\DownloadsController;
use App\Models\Download;
$download = Download::first();
app()->call([DownloadController::class, 'show'], ['download' => $download])
Please or to participate in this conversation.