Reputation: 1669
I am learning about dependency Injection in PHP, When I call this function in my Controller,
public function show(Request $request, $id)
{
$value = $request->session()->get('key');
}
it works, but how does Laravel know that the Request class being injected contains the current request made by the user, or a new instance of the Request class?
Upvotes: 0
Views: 8012
Reputation: 17388
If you look at public/index.php
, you can see Laravel create the initial $request
object by capturing the global request values.
// public/index.php ln 53
$request = Illuminate\Http\Request::capture()
The request is then passed to the Http\Kernel
for handling, where the $request
is subsequently passed to the sendRequestThroughRouter()
function. In this function, the request instance is bound to the application container.
// vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php ln 138
$this->app->instance('request', $request);
Now, every time the application injects a request instance, for example into your controller method, it knows specifically which object to use.
Upvotes: 2