Back to results for “Dependency injection”
PHP › MVC Architecture in PHP

What is the role of a Controller in the MVC pattern, and what responsibilities should typically be kept out of a controller to maintain good separation of concerns?

Beginner

Answer

A controller's primary responsibility is to receive an incoming request, coordinate with the appropriate model to retrieve or modify data, and determine which view should be rendered as a response, and it should generally avoid containing significant business logic itself, such as complex calculations or validation rules, which properly belong in dedicated model classes or separate service classes, keeping controllers thin and focused purely on request handling and coordination.
class OrderController {
    public function store(Request $request) {
        $order = $this->orderService->createOrder($request->validated());
        return redirect()->route('orders.show', $order);
    }
}
Real-world example An order processing controller delegates the actual complex order creation logic, including inventory checks and payment processing, to a dedicated OrderService class, keeping the controller itself simple and focused purely on handling the incoming request and returning an appropriate response.

Common follow-ups: What is a 'fat controller' and why is it generally considered a code smell?;Where should complex business logic ideally live if not directly in the controller?