首页 > 解决方案 > 在 laravel 应用程序中实现适配器设计模式

问题描述

我试图在我的 laravel 应用程序中实现适配器设计模式。我不得不使用更多的云来上传图片,例如(谷歌云存储,亚马逊 s3)。

1- 我创建了一个名为 ImageUploderInterface 的接口。

2- 我创建了 AmazonCloudFrontService 类和 GoogleCloudStorageService 类,它们中的每一个都实现了 ImageUploderInterface。

3-我创建了服务提供商来注册我的接口并从一个类(AmazonCloudFrontService 或 GoogleCloudStorageService)返回特定对象,这取决于环境变量。

4- 我在 config/app.php 中注册了我的服务提供商。

5-然后我尝试在我的控制器中注入 ImageUploderInterface 但是我有一个错误,接口没有退出。

图片上传界面

<?php

namespace App\Interfaces;

interface ImageUploderInterface
{   
   public function uploadImage($file, $uploadFolder);

   public function retrieveImage($fileName, $uploadFolder);
}

服务提供者

<?php

 namespace App\Providers;

 use Illuminate\Support\ServiceProvider;
 use App\Interfaces\ImageUploderInterface;
 use App\Services\AmazonCloudFrontService;
 use App\Services\GoogleCloudStorageService;

class ImageUploadServiceProvider extends ServiceProvider
{
/**
 * Register services.
 *
 * @return void
 */
public function register()
{
    $this->app->singleton(ImageUploderInterface::class, function ($app)         {
        switch ($app->make('config')->get('services.image-uploader')) {
            case 'amazon':
                return new AmazonCloudFrontService;
            case 'gcs':
                return new GoogleCloudStorageService;
            default:
                throw new \RuntimeException("Unknown upload image Service");
        }
    });
}
}

错误

消息:“类 App\Interfaces\ImageUploadInterface 不存在”,异常:“ReflectionException”,...} 异常:“ReflectionException”文件:“/var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php " line: 826 message: "Class App\Interfaces\ImageUploadInterface 不存在"

标签: phplaravellaravel-5php-7laravel-5.6

解决方案


推荐阅读