Backend Development Laravel Subjective
Sep 30, 2025

Explain Laravel's Service Container and Dependency Injection.

Detailed Explanation
The Service Container is Laravel's dependency injection container:\n\n**Binding Services:**\n```php\n// AppServiceProvider\npublic function register() {\n $this->app->bind('PaymentGateway', function ($app) {\n return new StripePaymentGateway(\n config('services.stripe.key')\n );\n });\n}\n```\n\n**Dependency Injection:**\n```php\nclass OrderController extends Controller {\n protected $paymentGateway;\n \n public function __construct(PaymentGateway $paymentGateway) {\n $this->paymentGateway = $paymentGateway;\n }\n \n public function process(Request $request) {\n return $this->paymentGateway->charge(\n $request->amount\n );\n }\n}\n```\n\n**Method Injection:**\n```php\npublic function show(User $user, PaymentGateway $gateway) {\n // Laravel automatically resolves dependencies\n}\n```
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback