Search code examples
phplaravelilluminate

Illuminate\Contracts\Filesystem\Factory is not instantiable


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?


Solution

  • 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
    );