Q1: Explain the Laravel Service Container and Dependency Injection.
Service Container: It is a powerful tool for managing class dependencies and performing dependency injection. It acts as a registry where you bind interfaces to implementations.
Dependency Injection: Instead of creating objects manually (new ClassName()), you type-hint the dependency in the constructor or method, and the container automatically resolves and injects it.
public function __construct(UserRepository $users) {
$this->users = $users;
}
Q2: What is a Facade and how does it work under the hood?
Concept: A Facade provides a static interface to classes that are available in the application's service container.
Under the hood: It uses the __callStatic() magic method to defer calls from the static facade to an instance of the underlying class resolved from the container.
Example: Cache::get('key') resolves the cache driver instance and calls get('key') on it.
Q3: What is the Repository Pattern and is it still relevant in Laravel?
Concept: A layer of abstraction between the domain logic and data mapping layers. It allows you to decouple your business logic from the specific database implementation (Eloquent).
Relevance: It is debated. For simple apps, Eloquent is sufficient and Repositories add boilerplate. For complex, enterprise apps where you might switch data sources or need strict testing boundaries, it is highly valuable.
Q4: Explain the SOLID principles in the context of Laravel.
S - Single Responsibility: A class should have one reason to change. (e.g., Don't put validation logic in your Controller; use Form Requests).
O - Open/Closed: Open for extension, closed for modification. (e.g., Use Laravel's Macros or Pipelines to add functionality without changing core code).
L - Liskov Substitution: Subtypes must be substitutable for their base types. (e.g., Any implementation of a PaymentGatewayInterface should work interchangeably).
I - Interface Segregation: Clients shouldn't depend on methods they don't use. (e.g., Split large interfaces into smaller ones).
D - Dependency Inversion: Depend on abstractions, not concretions. (e.g., Type-hint interfaces in constructors, not concrete classes).
Q5: What are Service Providers?
Role: The central place of all Laravel application bootstrapping. They are used to register services, event listeners, middleware, and routes.
Methods:
register(): Only bind things into the service container. No other logic.boot(): Called after all other service providers have been registered. You can access other services here (e.g., Event listeners, Routes).