Laravel 5软件包中的依赖项注入可使用或不可使用 [英] Dependency Injection in Laravel 5 Package To Use or Not To Use

查看:130
本文介绍了Laravel 5软件包中的依赖项注入可使用或不可使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Laravel 5开发一个程序包,我决定在使用Laravel的Core类时从程序包中的依赖项注入中受益,但是在阅读了更多内容并问了这个问题之后 Laravel 5软件包中依赖项注入的最佳方法

I am developing a package for Laravel 5, I decided to benefit from dependency injection in my package when using the Core classes of Laravel, but after reading more and also having asked this question Best approach for Dependency Injection in Laravel 5 package

现在,我想到了一个想法,如果我们大量使用Facades并调用FaceadeName:nameOfMethod之类的静态方法,那么容器实际上为我们创建了一个对象并调用了它的方法,因此在某种程度上使用了laravel依赖注入通过Facades也可以使用的课程几乎是没有用的.

Now I came up with the idea that if we are using Facades so much and calling static methods like FaceadeName:nameOfMethod the Container actually creates an object for us and calls its method, so to some extend using dependency injection for laravel for classes which are also available through the Facades are is almost useless.

例如,具有此类:

class MyController extends \App\Http\Controllers\Controller
{        
  public $text;
  public $lang;

 public function __construct()
 {         
   // Some codes here                            
}

public function myFunction(){
   $this->text = \Lang::get('package::all.text1');           
 }
}

这样做:

 App::bind('lang', function($app)
 {
    return new \Lang();
  });    

,然后在函数中:

public function myFunction()
{
    $lang = \App::make('lang');       
    $this->text = $lang::get('package::all.text1');           
}

几乎没有用,因为我们将某些东西绑定到已经绑定在那里的容器

is almost useless because we bind something to container that is already bound there

myFunction更改为

 public function myFunction(\Lang $lang){
     $this->text = $lang::get('package::all.text1');           
 }

同样,它看起来像方法注入,但是并没有带来太多优势.因此,最好不要在Laravel中将dependency injection用作Facades. 请让我知道我是否正确,如果我错了,请用正确的答案争论我的观点.

as well, it might look like method injection but it doesn't bring so much advantages. Therefore it would be better not to use dependency injection for Facades in Laravel. Please let me know if I am right or not and please argue my opinion with the right answer if I am wrong.

推荐答案

外观提供了一种通过类访问容器的方法,因此,当您访问\Lang::function()时,实际上是在调用app('translator')->function().因此,在上面的示例中,当您将Lang门面绑定到容器中时,您已经将其绑定了两次,这并不是您想要的.

Facades provide a way of accessing the container through a class, so when you're accessing \Lang::function() you're actually calling app('translator')->function(). So in you're above example when you're binding the Lang facade into the container, you've then bound it twice, which isn't quite what you want.

所有Laravel的功能已经绑定到容器中,可以通过调用app('object')进行访问.您可以在这里 http://laravel.com/docs/5.0/facades

All Laravel's functionality is already bound into the container and can be accessed by calling app('object'). You can see all the bindings here http://laravel.com/docs/5.0/facades

对于依赖项注入,您不应该尝试注入Facades,而应该是Facades已经引用的类.因此,例如,\Lang外观引用了Illuminate\Translation\Translator,该绑定到容器的名称为translator

For dependency injection, you shouldn't be trying to inject the facades, but rather the classes the facades are already referencing. So for example, the \Lang facade references Illuminate\Translation\Translator which is bound to the container as translator

在您的课程中,您可以执行以下操作

In your classes you can do the following

use App\Http\Controllers\Controller;
use Illuminate\Translation\Translator;

class MyController extends Controller
{
    protected $translator;

    // Dependency injection example
    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

    public function index()
    {
        $text = $this->translator->get('package::all.text1');
    }

    // Method injection example
    public function myFunction(Translator $translator)
    {
        $text = $translator->get('package::all.text1');
    }
}

这篇关于Laravel 5软件包中的依赖项注入可使用或不可使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆