Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do so many MVC web frameworks favor grouping multiple controller actions in a single class?

My experience is mostly limited to PHP, yet as far as I know both Rails and ASP.NET MVC have taken the same path.

The point is that nearly every web framework I've ever come across implements controller actions as methods, e.g. create, edit, show, etc. These methods reside in a single class like PostsController, but they hardly ever share state or dependencies, because only one of them gets called during the whole request.

That's why to me this approach seems quite unreasonable, as the class only acts as some kind of namespace. Seeing examples with large chunks of barely related controller action code composing even larger controller classes doesn't help either. Yet a lot of frameworks do exactly that, and only a few use a class for each action.

So the question is, why is it so? Perhaps it's subjective, but I believe that I may have missed an important advantage of this approach.

like image 898
Ignas R Avatar asked Jul 23 '10 21:07

Ignas R


People also ask

Can we have multiple controllers in MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.

Is MVC framework still used?

Today the MVC pattern is used for modern web applications because it allows the application to be scalable, maintainable, and easy to expand.

What is the use of controller in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

What is a controller in web development?

A controller is a program component that serves as a mediator between a user and application and handles business-related tasks triggered in ASP.NET pages. A controller is used for scripting exposed and middle-tier endpoints for expected user actions and results.


1 Answers

I would argue that the MVC design pattern in general dictates this approach to building controllers. The main purpose of the controller is to provide the appropriate "wiring" between the associated view and the models it needs to interact with, along with any business logic needed to handle the input from the view. Controllers should just be a thin layer between these other components. For example, Wikipedia describes the controller as follows:

The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

I do agree that controllers in other, non-web environments do maintain state, but the reason for the lack of state in PHP, for example, is simply that HTTP is a stateless protocol. Using MVC in this environment is inherently going to result in controllers that don't maintain state.

like image 186
Clayton Avatar answered Nov 02 '22 05:11

Clayton