gpt4 book ai didi

php - Laravel 编写接口(interface)的最佳方式

转载 作者:行者123 更新时间:2023-12-05 03:09:41 24 4
gpt4 key购买 nike

我需要有关此方法的建议以编写接口(interface)。

场景:我需要实现一个虚拟类 ImageUploader 然后在构造函数上接收一个接口(interface)并将图像保存在我的目录中。这是出于学习目的,所以如果我以正确的方式进行操作,我需要您的建议:

这是我在 Laravel 5.3 框架上的实现:

1:实现的虚拟接口(interface),因此我可以创建不同的方式来存储我的图像

//dummy interface

namespace App\Lib\ImageUploader\Drivers;

interface ImageInterface
{
public function hello();
}

2:这里有两个实现我的接口(interface)的驱动程序。每个都有自己的方法hello(在现实生活中,例如每个类可能有自己的保存方法用于任何类型的驱动程序)

// Avatar Driver Class

namespace App\Lib\ImageUploader\Drivers;

class AvatarImage implements ImageInterface
{
public function hello()
{
return 'I am a AvatarImage';
}
}

还有一个类,例如BackgroundImage可以保存用户上传图片的桌面版和移动版:

// Background Driver Class


namespace App\Lib\ImageUploader\Drivers;

class BackgroundImage implements ImageInterface
{
public function hello()
{
// this is a dummy method, in real life this class will save 2 images (desktop + mobile)
return 'I am a BackgroundImage';
}

}

这是我的 ImageUploader 类,具有“Programming to interface”策略:

// ImageUploader.php
// this class will implement all methods that I need for manage saving operations

namespace App\Lib\ImageUploader;
use App\Lib\ImageUploader\Drivers\ImageInterface;


class ImageUploader
{

protected $driver;

public function __construct(ImageInterface $driver)
{
$this->driver = $driver;
}

public function save()
{
return $this->driver->hello();
}
}

现在我创建自己的 Laravel 框架服务提供者:

namespace App\Providers;

use App\Lib\ImageUploader\Drivers\AvatarImage;
use App\Lib\ImageUploader\Drivers\BackgroundImage;
use App\Lib\ImageUploader\ImageUploader;
use Illuminate\Support\ServiceProvider;

class ImageUploadServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}

/**
* Register the application services.
*
* @return void
*/

public function register()
{
$this->registerAvatar();
$this->registerBackground();
}

protected function registerAvatar(){
$this->app->bind('AvatarUploader', function () {
return new ImageUploader(new AvatarImage());
});
}

protected function registerBackground(){
$this->app->bind('BackgroundUploader', function () {
return new ImageUploader(new BackgroundImage());
});
}
}

最后,我尝试在我的 Controller 中使用我的类,例如当用户尝试上传您的头像图片或新背景图片时:

// this will produce "I am a AvatarImage" in real life this line create thumbnail and will save my image in my local directory

public function store(Request $request){
(App::make('AvatarUploader'))->save();
}

有更好的方法吗?对我的实现有什么建议吗?

最佳答案

看看https://laravel.com/docs/5.3/container#binding-interfaces-to-implementationshttps://laravel.com/docs/5.3/container#contextual-binding .

因此在您的服务提供者逻辑中,您可以指定 -

$this->app->when(AvatarController::class)
->needs(ImageInterface::class)
->give(function () {
return new AvatarUploader();
});

$this->app->when(BackgroundController::class)
->needs(ImageInterface::class)
->give(function () {
return new BackgroundImage();
});

然后在您的 Controller 或其他类中,只需依赖注入(inject) ImageInterface 接口(interface)而不是具体类。

class AvatarController extends Controller
{
protected $imageInterface;
public function __construct(ImageInterface $imageInterface)
{
$this->imageInterface = $imageInterface;
}

public function store()
{
return $this->imageInterface->hello(); // returns "I am an Avatar Image"
}
}

class BackgroundController extends Controller
{
protected $imageInterface;
public function __construct(ImageInterface $imageInterface)
{
$this->imageInterface = $imageInterface;
}

public function store()
{
return $this->imageInterface->hello(); // returns "I am a Background Image"
}
}

关于php - Laravel 编写接口(interface)的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com