3

I try to use the stand alone components of laravel. In this case I try to use illuminate/http. It works fine except the file save after upload is throwing an exception.

( ! ) Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Filesystem\Factory] is not instantiable. in /var/www/vendor/illuminate/container/Container.php on line 978
( ! ) Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Filesystem\Factory] is not instantiable. in /var/www/vendor/illuminate/container/Container.php on line 978

This is my bootstrapping:

use Illuminate\Http\Request;

/** @var Request $request */
$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER
);

And this is the part where I try to store a form post file on my local storage:

$request->image->store(APP_PUBLIC.'resources/assets/images/homepage/slider/test.jpg');

I tried to inject the Illuminate\Filesystem\Filesystem() as Factory into the Reqest object, but it seems not to work.

How to inject the Filesystem factory into the HTTP component?

lin
  • 17,956
  • 4
  • 59
  • 83

2 Answers2

2

I fixed it by correctly adding the necessary factories, configs and register them.

composer.json

{
    "require": {
        "illuminate/validation": "^6.16",
        "illuminate/filesystem": "^6.16",
        "illuminate/translation": "^6.16",
        "illuminate/http": "^6.16",
        "illuminate/config": "^6.16",
        "jenssegers/blade": "^1.2",
        "league/flysystem": "^1.0"
    },
}

Bootstrapping the filemanager:

use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Http\Request;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Config\Repository;

$instance = Container::getInstance();
$instance->bind('config', function () use ($instance) {
    return new Repository([
        'filesystems' => [
            'default' => 'local',
            'disks' => [
                'local' =>[
                    'driver' => 'local',
                    'root' => '/absolut/path/to/upload/dir/,
                ]
            ]
        ]
    ]);
});

$instance->bind(FilesystemFactory::class, function () use ($instance) {
    return new FilesystemManager($instance);
});

/** @var Request $request */
$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER
);
lin
  • 17,956
  • 4
  • 59
  • 83
0

Probably the easiest way to save a file on disk using Illuminate\Filesystem\Filesystem::move() method or other methods from it, when one is working with standalone Illuminate\Http\Request component and wants to work with file-system. See the code below:

<?php

use Illuminate\Http\Request;
use Illuminate\Filesystem\Filesystem;

// Bootstraps request object
$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER
);


// Illuminate\Http\UploadedFile is available on $request->image
$path = $request->image->path();
$extension = $request->image->extension();

// Don't use destination thus. It's for demonstration purpose only
// Make sure upload directory is writable         
$destination = 'uploads/' . mt_rand() . '.' . $extension;

// Here is the moment  
$file = new Filesystem();
$file->move($path, $destination);
unclexo
  • 3,691
  • 2
  • 18
  • 26