0
0
Laravelframework~20 mins

Dependency injection in controllers in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when injecting a service in a Laravel controller?
Consider a Laravel controller that injects a service class via its constructor. What will be the output when calling the controller method that uses this service to return a greeting message?
Laravel
<?php
namespace App\Http\Controllers;
use App\Services\GreetingService;

class WelcomeController extends Controller
{
    protected $greetingService;

    public function __construct(GreetingService $greetingService)
    {
        $this->greetingService = $greetingService;
    }

    public function greet()
    {
        return $this->greetingService->sayHello();
    }
}

// GreetingService.php
namespace App\Services;

class GreetingService
{
    public function sayHello()
    {
        return 'Hello, Laravel!';
    }
}
A"Hello, Laravel!"
BAn error: Class 'GreetingService' not found
C"Hello from controller!"
DNull or empty response
Attempts:
2 left
💡 Hint
Think about how Laravel automatically resolves classes injected in the constructor.
state_output
intermediate
2:00remaining
What is the value of the injected repository property after controller instantiation?
Given a Laravel controller that injects a UserRepository interface, what will be the value of the repository property after the controller is created?
Laravel
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepositoryInterface;

class UserController extends Controller
{
    private UserRepositoryInterface $repository;

    public function __construct(UserRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    public function index()
    {
        return $this->repository->allUsers();
    }
}
AAn empty array
BAn instance of a class implementing UserRepositoryInterface
CAn error: Cannot instantiate interface UserRepositoryInterface
DNull because interfaces cannot be instantiated
Attempts:
2 left
💡 Hint
Laravel binds interfaces to concrete classes in the service container.
📝 Syntax
advanced
2:00remaining
Which option correctly injects a service into a Laravel controller method?
You want to inject a Logger service directly into a controller method instead of the constructor. Which code snippet correctly achieves this?
Laravel
<?php
namespace App\Http\Controllers;
use App\Services\Logger;

class LogController extends Controller
{
    public function logAction( /* what goes here? */ )
    {
        // Use $logger to log a message
        $logger->info('Action logged');
        return 'Logged';
    }
}
ALogger $logger
Bpublic Logger $logger
CLogger $logger = new Logger()
DLogger $logger = null
Attempts:
2 left
💡 Hint
Method injection requires type hinting the parameter without visibility or default values.
🔧 Debug
advanced
2:00remaining
Why does this Laravel controller injection cause an error?
Examine the following controller code. Why does Laravel throw a binding resolution exception when trying to instantiate this controller?
Laravel
<?php
namespace App\Http\Controllers;
use App\Services\PaymentGateway;

class PaymentController extends Controller
{
    public function __construct(PaymentGateway $gateway)
    {
        $this->gateway = $gateway;
    }
}
AThe constructor is missing a return type
BThe property $gateway is not declared
CPaymentGateway is not bound in the service container
DThe namespace declaration is incorrect
Attempts:
2 left
💡 Hint
Check if the service container knows how to create PaymentGateway.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using dependency injection in Laravel controllers?
Choose the best explanation for why dependency injection is used in Laravel controllers.
AIt prevents the use of service providers
BIt forces all dependencies to be global singletons
CIt makes controllers run faster by caching dependencies
DIt allows automatic resolution and easier testing by decoupling dependencies
Attempts:
2 left
💡 Hint
Think about how dependency injection affects code flexibility and testing.