0

I am developing a package for Laravel 5, I decided to benefit form Dependency Injection in my package and it is easy to implement in Laravel particularly for constructor injection, but when it comes to Facades some new issues arise. when we have classes like

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

   public function __construct(\Lang $lang)
   {         
      $this->lang = $lang;
   }

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

The code above doesn't work since the Lang is a Facade, therefore we have to change the code to this:

  use Illuminate\Translation\Translator

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

   public function __construct(Translator $translator)
   {         
      $this->translator = $translator;
   }

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

The Question: I need to know what are the advantages and disadvantage of using the facades against using the constructor injection

I also have checked this question: laravel - dependency injection and the IoC Container and laready asked this question: Dependency Injection in Laravel 5 Package To Use or Not To Use

you just can help as well with confirming the answer of the first question as well

Community
  • 1
  • 1
Siavosh
  • 2,314
  • 4
  • 26
  • 50
  • 1
    I don't think you should inject a facade. AFAIK injections should only be made with interfaces. – Hkan Mar 07 '15 at 22:41
  • I'm also having issues trying to use the Cache Facade within a package I'm building so will be interested in answers to this question. – Ian Lewis Oct 16 '15 at 08:27

0 Answers0