为了账号安全,请及时绑定邮箱和手机立即绑定

Illuminate\Contracts\Filesystem\Factory 不可实例化

Illuminate\Contracts\Filesystem\Factory 不可实例化

PHP
缥缈止盈 2022-10-09 17:46:03
我尝试使用 laravel 的独立组件。在这种情况下,我尝试使用Illuminate/http。它工作正常,除了上传后保存的文件抛出异常。( ! ) 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这是我的引导:use Illuminate\Http\Request;/** @var Request $request */$request = new Request(    $_GET,    $_POST,    [],    $_COOKIE,    $_FILES,    $_SERVER);这是我尝试在本地存储中存储表单发布文件的部分:$request->image->store(APP_PUBLIC.'resources/assets/images/homepage/slider/test.jpg');我试图将Illuminate\Filesystem\Filesystem()as Factory 注入到 Reqest 对象中,但它似乎不起作用。如何将文件系统工厂注入 HTTP 组件?
查看完整描述

2 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

我通过正确添加必要的工厂、配置并注册它们来修复它。


作曲家.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"

    },

}

引导文件管理器:

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

);


查看完整回答
反对 回复 2022-10-09
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

当使用独立组件并希望使用文件系统Illuminate\Filesystem\Filesystem::move()时,可能是使用方法或其他方法将文件保存在磁盘上的最简单方法。Illuminate\Http\Request请看下面的代码:


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


查看完整回答
反对 回复 2022-10-09
  • 2 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信