无法在Laravel 6中使用依赖项注入

时间:2020-05-22 12:25:42

标签: laravel dependency-injection google-drive-api laravel-6

我正在使用laravel6。我有一个称为DriveServiceProvider的服务提供程序类。此类具有与Google Drive连接所需的所有依赖关系。以下是我的服务提供商

     <?php

  namespace App\Http\Controllers\Cloud;
  use App\Http\Controllers\Controller;
  use App\Providers\DriveServiceProvider;
  use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
  use Illuminate\Support\Facades\Cache;
  use League\Flysystem\Filesystem;


  class uploadFileController extends Controller{

   public $filesystem;

   //Attempting Dependency Injection
   public function __construct(DriveServiceProvider $filesystem)  {


    $this->filesystem=$filesystem;

    }

   public function createFolder(){

    $directoryName='Demo';

    $this->filesystem->createDir($directoryName);

     //return $$filesystem->folder_id;

    }

   public function deleteFolder($id){

     $this->filesystem->delete();

     }






   }

我想将此服务提供者传递给控制器​​,以下是我的控制器

  Illuminate\Contracts\Container\BindingResolutionException
  Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class     
  Illuminate\Support\ServiceProvider

当我运行脚本时,它抛出了错误

            |05/12/2020                 |05/13/2020
Created By  |Accuracy   Achieved %      |Accuracy   Achieved %
------------------------------------------------------------------
John        |  90        105            |   85       105

我的代码有什么问题?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

有一种更简单的方法可以在Laravel安装中实施Google云端硬盘。

composer require nao-pon/flysystem-google-drive

filesystems.php中包含您的Google配置。

    'google' => [
        'driver' => 'google',
        'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
    ],

现在您应该可以像这样获取文件了。

Storage::disk('google')->get('yourfile');

这受以下tutorial的启发。