Facades Overview0:00
Facades are one of the things many people are confused about. They look like static classes you can call from anywhere, but they actually aren't. They are just a way to resolve dependencies from the Laravel dependency container without having to require them in a class constructor. Let's look into facades and demystify their magic. Inside our command here, let's use the dump method, and as a method argument, we will call db,
Querying with DB Facade0:27
let's use the dump method, and as a method argument, we will call db, as in Illuminate\Support\Facades\db. And then we call the table method, we are querying on the users table, and then we call get to get the records from the database. Now, let's run the command, and we get empty results. That's just because the table is empty, but the query actually ran.
That's just because the table is empty, but the query actually ran. Now, if we go take a look at this DB class, we won't see a table method there, only a get facade accessor method that returns a string, DB. And this DB class extends a Facade class. Let's go check it out. Let's see if we can find the table method there, and nothing.
Static Call Magic Method1:17
Let's see if we can find the table method there, and nothing. So how does Laravel learn we are calling the table method of our database component, not a static method on the DB class? Let's scroll down all the way, and we see a call static method defined. This is a php magic method. It allows us to handle calls to static methods that don't actually exist in the class.
It allows us to handle calls to static methods that don't actually exist in the class. In the case of the Facade class here, we are calling a getFacadeRoot method, which returns some instance, and then we call the static method name as a dynamic method on that instance and pass all provided arguments. So here, the Facade acts as a proxy to some instance. How is this instance resolved?
Resolving via Container Binding2:04
So here, the facade acts as a proxy to some instance. How is this instance resolved? Let's find out. Let's check this getFacadeRoot method. It calls another method named resolveFacadeInstance, and provides the result of the getFacadeAccessor method as an argument. This getFacadeAccessor method was the only method defined on the DB facade that we checked earlier.
was the only method defined on the DB facade that we checked earlier. It returned a string with the value db. So now let's check this method and inspect this line here. It interacts with our Laravel application's dependency container, and it asks it for a binding with the name of the facade accessor. In our case, that binding is named db.
with the name of the facade accessor. In our case, that binding is named db. So here, it resolves the db binding from the container and later proxies static method calls to it. And we can see here that it also stores the reference to the object returned by the container in a local static array. That makes it easier in the future to know what instance each facade is proxying without getting back to the container.
to know what instance each facade is proxying without getting back to the container. If the instance was already in the local array, it just returns it right away. In some facades, this caching behavior is disabled. And in that case, the method just returns the instance from the container. So now let's go back to the console file and remove the full class name, just keep the root class name only.
and remove the full class name, just keep the root class name only. Run the command, and it still works. That's due to the aliasing magic that we explained in the previous lesson. All facades are aliased so you can easily use them in your code. So now that you know how facades work, they won't look like magic anymore. They are just a way to proxy calls.
Facade Usage Tradeoffs4:02
they won't look like magic anymore. They are just a way to proxy calls to bindings in the service container. You can use them anywhere in your code, but you need to be mindful about them. If you use many facades in a single class, that could mean the class is growing too large and has many dependencies. That's a sign this class needs some refactoring. To refactor the class or not, that's your decision.
That's a sign this class needs some refactoring. To refactor the class or not, that's your decision. To use facades in your code base or not, that's also your decision. I will see you in the next lesson.
