Laravel编程界面的最佳方式

时间:2017-02-05 16:41:18

标签: php laravel interface laravel-5.3

我需要建议使用这种方法来编程接口。

方案: 我需要实现一个虚拟类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();   
}

有更好的方法吗?关于我的实施的任何建议?

1 个答案:

答案 0 :(得分:1)

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

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

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" } } 接口而不是具体的类。

    class MiDragListener implements OnDragListener
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
        int accion = event.getAction();

        switch (accion)
        {
        case DragEvent.ACTION_DRAG_STARTED:
        case DragEvent.ACTION_DRAG_EXITED:
            v.setScaleX((float)1.0);
            v.setScaleY((float)1.0);
            break;

        case DragEvent.ACTION_DRAG_ENTERED:
            v.setScaleX((float)2.0);
            v.setScaleY((float)2.0);
            break;

        case DragEvent.ACTION_DRAG_ENDED:
            v.setScaleX((float)1.0);
            v.setScaleY((float)1.0);
            break;

        case DragEvent.ACTION_DROP:
            break;

        default:
            break;
        }

        return true;
    }
} // end MiDragListener
相关问题