php - 在 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 不存在"
解决方案
推荐阅读
- javascript - 如何在 React 组件之间传递数据而不渲染子组件
- regex - 删除引号之间的引号
- java - 有人用 Spring Cloud Gateway 实现 Jaeger 吗?
- json - 如何在 Jasper Studio 中计算表格和表格返回值的总和?
- eiffel - 是否在后代重新定义时调用了救援子句?
- c# - Entityframework 6:如何将自定义类型映射到 SQL 表
- datetime - 如何在 ESQL 中将 TIMESTAMP 转换为 UTC 毫秒
- python - 关于 Julia 中的 ndarray 创建:Stacking in extra dimension
- kubernetes - 为什么我的 autofs 服务不在我的 linux 容器上运行?
- excel - 我需要为第 5-200 行重复 VBA 代码,但不知道该怎么做