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