Combine smart routing with seamless DI
We’ve gone through a few dependency injection topics in the last couple of articles, but now we want to start getting more specific. We have finally entered a more advanced portion of Laravel and will start focusing on specific topics within specific areas of Laravel. The next few articles will deal with routing. In this article, we’ll focus on dependency injections in routes, which is really straightforward.
In your route you may type-hint any dependency into your callback signature. If you’re using controllers, the type hint will occur at the method level.
use Illuminate\Http\Request;
Route::get('/dependency-route-example', function (Request $request) {
return $request->input('name');
});
You can now visit the route and the Request
dependency will be automatically injected into the route.
http://0.0.0.0/dependency-route-example?name=Dino
Dependency Injection with Other Parameters
You may wish to pass an argument to your route and also utilize dependency injection through type-hinting. You can do so. Just make sure that your parameters are declared after all of your dependency type-hints.
use Illuminate\Http\Request;
Route::get('/dependency-route-example/{id}', function (Request $request, $id) {
return $id . ": " . $request->input('name');
});
http://0.0.0.0/dependency-route-example/33?name=Dino
Visiting the route above will produce: 33: Dino
.
Can we inject multiple dependencies and use arguments? Of course. In the previous article, we created the app/Services/CapitalizeStringService
class.
<?php
namespace App\Services;
class CapitalizeStringService
{
public function capitalize($string)
{
return strtoupper($string);
}
}
To inject and use this class, we can just type hint it in our route, before our URL arguments.
Route::get('/dependency-route-example/{id}',
function (Request $request, CapitalizeStringService $capitalizeStringService, $id)
{
return $id . ": " . $capitalizeStringService->capitalize( $request->input('name') );
}
);
Visiting our route now produces a capitalized version of the name
string: 33: DINO
.
As long as you trust that Laravel will take care of dependency injection when you type-hint it, there’s not much more that you need to concern yourself with. We’ll fully cover dependency injection in controllers when we get to the controller section.
Laravel Series
Continue your Laravel Learning.
Implement effective DI patterns for cleaner, testable Laravel code
Laravel – P49: Dependency Injection
In part 49 of our Laravel series, learn how to apply Dependency Injection in practice. Discover how to structure your project for clearer separation of concerns, enhance testability, and simplify code maintenance by fully leveraging Laravel’s built-in DI container.
Combine smart routing with seamless DI
Laravel – P50: Routing and Dependency Injection
In part 50 of our Laravel series, see how routing and the framework’s dependency injection container work together. Learn to inject services directly into route closures and controllers to keep code clean, testable, and flexible.
Let Laravel fetch your models automatically
Laravel – P51: Route Model Implicit Binding
In part 51 of our Laravel series, master route‑model implicit binding. Discover how Laravel automatically resolves models from route parameters, simplifies controller logic, and adds type‑safe, readable code to your applications.