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

问题描述 投票:0回答:1

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

      <?php

     use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
     use League\Flysystem\Filesystem;
     use Illuminate\Support\ServiceProvider;
     use Illuminate\Support\Facades\Storage;


     class DriveServiceProvider extends ServiceProvider
     {

       public function register(){


       }

       public function boot()

       {

       Storage::extend('google',function( $app,$config){

        $client=new \Google_Client();
        $client->setClientId($config['clientId']);
        $client->setClientSecret($config['clientSecret']);
        $client->refreshToken($config['refreshToken']);
        $service=new \Google_Service_Drive($client);
        $adapter=new GoogleDriveAdapter($service,$config['folderId']);
        return new FileSystem($adapter);
       });



     }
 }

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

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

我的代码怎么了?任何帮助表示赞赏。

laravel dependency-injection google-drive-api laravel-6
1个回答
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的启发。

© www.soinside.com 2019 - 2024. All rights reserved.