Bassone's avatar

What are the advantages by using facades?

Hello! I'm new to laravel, and there are things I don't understand well. I know what are facades and how they works, but I can't understand why I should define a facade rather than define a class with static methods. What are the motivations behind this choice? Also, in the laravel documentation I often see "Facades vs Contracts", as one thing exclude the other, but in the same documentation we also see that we could have a corresponding facade for some contract. Plus, I could define a facade that implements a contract (interface). Then, sometimes why should I see them as two opposite things? Thanks!

0 likes
1 reply
bobbybouwmann's avatar

You can see a facade as a short cut to get access to a certain method, without having to make the method static. The downside of static methods is that they also need to call other static methods. Most of the time you don't want that in your class. The advantage of a facade is that you can still code your class like a normal class, but you don't have to think about dependency injection inside your code and you simply get direct access to all available methods.

Contracts are more for dependency injection. You inject the contract and the container will figure out what concrete class is connected to it. This is most of the time defined in one of your ServiceProviders. This is very useful to always get the correct implementation, but often it requires a constructor and more structure in your application.

So facades are basically just short cuts, but both solutions are perfectly fine ;)

1 like

Please or to participate in this conversation.