تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Reflection in Laravel0:00

Laravel Contributors led by Taylor Otwell invest a lot in developer experience. Laravel wants you to spend less time writing code and more time on building the business logic in the most performant way. It takes every possible opportunity the PHP language allows to enhance the developer experience. And one of the things that PHP allows is the ability to examine the code at runtime and modify the behavior. In other words, the program can reflect on your code and decide what to do. This is called Reflection,

Injecting Application into Commands0:31

In other words, the program can reflect on your code and decide what to do. This is called Reflection, and Laravel uses the Reflection API to enhance the developer experience. In this video, we will inspect the areas where Laravel does that. So, let's go. Imagine that we need to use the instance of the Laravel application in our console command. Let's say we want to extract the current environment the application is running inside. And to do that, we may add an argument to our closure here,

the application is running inside. And to do that, we may add an argument to our closure here, and type hint it with Illuminate\Contracts\Foundation\Application. And we use $app for the variable name. Then, inside the closure, we can dump the environment. So, $app, environment. Run the command. And it works. How? The way this works is that Laravel uses the Reflection API.

How? The way this works is that Laravel uses the Reflection API to examine our closure and see the parameters defined. It detects that the closure requires an instance of the application and provides one. You can require as many dependencies as you want. Laravel will recognize that and use the application container to resolve them. You will see the same pattern used in many places where you can resolve dependencies from the container by type hinting, like route actions, for example.

Binding Console Input Parameters1:55

where you can resolve dependencies from the container by type hinting, like route actions, for example. Now, that's not all. Let's say our command here accepts Input. An Input, for example. Here is how we could define this required command Input. We do that here. Name. And to access the value provided by the user, we can add an argument to the closure with the same name as our Input.

And to access the value provided by the user, we can add an argument to the closure with the same name as our Input. So here, name. Now, let's dump this variable. And then we run the command. We need to provide the Input, Mohamed. And here we go. Laravel uses Reflection to learn that we want to bind the value of a command Input called name to the closure argument called name. This pattern is also used in many places like route controller actions.

Contextual Binding for Controllers3:07

your life as a programmer will be much easier. Before we finish the video, let me show you another magic trick in this regards. Let's check our routes file. We have this route using a controller named WelcomeController. If we check the controller, we can see that it depends on a string primitive called server. This controller needs to know the current web server the machine is running on. And if we go curl the URL, so curl http://laracasts.test,

And if we go curl the URL, so curl http://laracasts.test, we get the server in the response. How did Laravel know that our server constructor property here refers to the current web server? Let's go to the AppServiceProvider. Check the register method. We see here that I've instructed Laravel that when the WelcomeController asks for a server, give it the current web server from the request.

Event Listener Auto-Registration4:07

that when the WelcomeController asks for a server, give it the current web server from the request. This is called contextual binding. And it's magic. Let's do one more magic trick. Let's say we want to print the name of the current route the router matched. And to do that, we will listen to the routeMatched event and bring the route name. So app resolve the eventDispatcher from the container and then call listen and provide a closure.

So app resolves the event dispatcher from the container and then calls listen and provides a closure. The closure accepts the event route matched and assigns it to a variable named event. And then inside the closure, we echo route name event route getName and add a new line. Let's call our app curl http://laracasts.test. And here we go. We have the route name printed.

And here we go. We have the route name printed. If we look at the listener we registered, we can see that Laravel detected that this listener is listening to the routeMatched event by using reflection. It examined the closure parameters and registered the listener to the event we want. That's very cool. Again, magic is confusing to those who aren't familiar with it. But once you know, you aren't confused anymore.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟