5

I don't understand the point of Facade if you are going to inject your class into a controller as part of IoC.

Say I have a custom facade called PostHelper. I have the following 2 functions:

class PostHelper
{
    public function __construct() {} 

    public function all() {}

    public function get($id) {} 
}

To use this helper, with and without facades you would (say in your controller)

// Without Facade
$helper = new PostHelper();
return $helper->all();

// With Facade
return PostHelper::all();

But, this is bad practice since I cannot mock the PostHelper when testing it. Instead, I would pass it to the constructor of my controller:

class HomeController extends BaseController
{
    private $PostHelper;

    public function __construct(PostHelper $helper)
    {
        $this->PostHelper = $helper;
    }

    public function index()
    {
        return $this->PostHelper->all();
    } 
}

In the constructor, I could have just used $this->PostHelper = new $helper() if I had not created a Facade. Either way, I am never using the static feel of a Facade when using DI.

So what is the point of using a Facade?

Kousha
  • 32,871
  • 51
  • 172
  • 296

1 Answers1

1

To quote the documentation:

Facades provide a "static" interface to classes that are available in the application's IoC container. Laravel ships with many facades, and you have probably been using them without even knowing it! Laravel "facades" serve as "static proxies" to underlying classes in the IoC container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

It's just another way of using outside dependencies without needing to understand dependency injection, how to register and/or fetch items using the IoC container, etc. They are a convenience, particularly for inexperienced developers or those new to Laravel.

If you are going to inject your dependencies (which you should), you don't need facades.

You can actually mock facades, but I would still practice normal dependency injection.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
  • So in real applications, Facades are NOT useful as you should always inject your dependencies anyway. – Kousha Dec 08 '14 at 18:15
  • 1
    If you want to look at it that way, sure. I don't subscribe to the puritan "PHP apps are junk unless they're written exactly like this" mentality, because that opinion will always differ amongst developers. I encourage the use of DI within your domain code, but prefer to use facades within controllers, due to ease of development and controllers being framework-specific (usually, doesn't have to be). – Aken Roberts Dec 08 '14 at 19:30
  • If you use the (custom) facades in your controller, how do you write unit test for them? Is it easy to mock them? – Kousha Dec 08 '14 at 20:18
  • I haven't bothered testing controller code. My controllers typically just funnel request data to services in the domain layer, which are tested on their own. I couldn't tell you if mocking facades is easy or not (the docs make it seem like it isn't a big deal, though). – Aken Roberts Dec 08 '14 at 21:02