0
0
LaravelHow-ToBeginner · 4 min read

How to Use Dependency Injection in Controller in Laravel

In Laravel, you use dependency injection in controllers by adding the class type you want injected as a parameter in the controller's constructor or method. Laravel's service container automatically resolves and provides the instance when the controller is created or the method is called.
📐

Syntax

Dependency injection in Laravel controllers is done by declaring the dependency as a type-hinted parameter in the constructor or controller method. Laravel's service container then automatically provides the required instance.

  • Constructor Injection: Inject dependencies via the controller's constructor.
  • Method Injection: Inject dependencies directly into controller methods.
php
class UserController extends Controller
{
    protected $service;

    // Constructor Injection
    public function __construct(UserService $service)
    {
        $this->service = $service;
    }

    // Method Injection
    public function show(UserService $service, $id)
    {
        return $service->getUser($id);
    }
}
💻

Example

This example shows a controller using constructor injection to get a UserService instance. Laravel automatically creates and injects the UserService when the controller is used.

php
<?php

namespace App\Http\Controllers;

use App\Services\UserService;

class UserController extends Controller
{
    protected UserService $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function show(int $id)
    {
        return response()->json($this->userService->getUser($id));
    }
}

// UserService.php
namespace App\Services;

class UserService
{
    public function getUser(int $id): array
    {
        // Simulate fetching user data
        return ['id' => $id, 'name' => 'User '.$id];
    }
}
Output
{"id":1,"name":"User 1"}
⚠️

Common Pitfalls

  • Not type-hinting the dependency class correctly causes Laravel to fail resolving it.
  • Forgetting to register interfaces or classes in the service container if they require custom bindings.
  • Injecting too many dependencies in the constructor can make the controller hard to maintain.
  • Using dependency injection in controller methods requires the method to be called by Laravel's routing system to resolve dependencies.
php
<?php
// Wrong: Missing type hint, Laravel cannot inject
public function __construct($userService) {
    $this->userService = $userService;
}

// Right: Correct type hint for injection
public function __construct(UserService $userService) {
    $this->userService = $userService;
}
📊

Quick Reference

  • Use constructor injection for dependencies used across multiple methods.
  • Use method injection for dependencies needed only in specific methods.
  • Always type-hint the class or interface you want injected.
  • Register bindings in App\Providers\AppServiceProvider if using interfaces.

Key Takeaways

Type-hint dependencies in controller constructors or methods for automatic injection.
Laravel's service container resolves and provides the required instances.
Always ensure classes or interfaces are properly registered if custom bindings are needed.
Constructor injection is best for dependencies used throughout the controller.
Method injection works well for dependencies needed only in specific controller actions.