Challenge - 5 Problems
Laravel Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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!'; } }
Attempts:
2 left
💡 Hint
Think about how Laravel automatically resolves classes injected in the constructor.
✗ Incorrect
Laravel's service container automatically injects the GreetingService instance into the controller constructor. Calling sayHello() returns the string 'Hello, Laravel!'.
❓ state_output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Laravel binds interfaces to concrete classes in the service container.
✗ Incorrect
Laravel's service container resolves the interface to a concrete class bound in a service provider, so the repository property holds an instance of that class.
📝 Syntax
advanced2: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'; } }
Attempts:
2 left
💡 Hint
Method injection requires type hinting the parameter without visibility or default values.
✗ Incorrect
Laravel supports method injection by type hinting the parameter. Visibility keywords or default values are not allowed in method parameters.
🔧 Debug
advanced2: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;
}
}Attempts:
2 left
💡 Hint
Check if the service container knows how to create PaymentGateway.
✗ Incorrect
If PaymentGateway is a class that requires parameters or is not bound in the container, Laravel cannot resolve it automatically, causing the error.
🧠 Conceptual
expert2: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.
Attempts:
2 left
💡 Hint
Think about how dependency injection affects code flexibility and testing.
✗ Incorrect
Dependency injection decouples the controller from concrete implementations, enabling easier swapping of dependencies and unit testing.