簡體   English   中英

Laravel編程接口的最佳方法

[英]Laravel Best way to programming an interface

我需要有關此方法的接口編程建議。

場景:我需要實現一個虛擬類ImageUploader然后在構造函數上接收一個接口,並將圖像保存在我的目錄中。 這是出於學習目的,因此,如果我以正確的方式進行操作,則需要您的建議:

這是我在Laravel 5.3框架上的實現:

1:實現虛擬界面,因此我可以創建不同的方式來存儲圖像

//dummy interface

namespace App\Lib\ImageUploader\Drivers;

interface ImageInterface
{
   public function hello();
}

2:這里有兩個驅動程序實現了我的界面。 每一個作為自己的方法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類,具有“編程到接口”策略:

// 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());
       });
    }
}

最后,我嘗試在控制器中使用我的類,例如,當用戶嘗試上傳您的頭像圖像或新的背景圖像時:

// 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();
          });

然后在您的控制器或其他類中,僅依賴項注入ImageInterface接口而不是具體的類。

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"
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM