Models in Symfony2, and other MVC frameworks? Models in Symfony2, and other MVC frameworks? symfony symfony

Models in Symfony2, and other MVC frameworks?


  1. Symfony2 does not have the traditional "Model" part of MVC like other frameworks do. Even Entities/Documents from ORM/ODM Doctrine are not part of the framework itself nor does Symfony2 depend on it.

    As Fabien (creator of Symfony framework) wrote on his blog,

    "It's up to you to create your model by hand or use any other tool, like an ORM" ... "I don't like MVC because that's not how the web works. Symfony2 is an HTTP framework; it is a Request/Response framework."

    It was hard for me to understand just reading, but I understood what he meant when I actually started programming in Symfony2.

    A service in Symfony2 on the otherhand is just an object that performs a global task. Router, doctrine, logger, mailer are some of the many services that come preloaded with Symfony2. You can access services from any part of your code.

  2. Symfony2 services are completely different from web services. Symfony2 services are meant to be used within your system while web services are meant to be used from machine to machine via REST api for example. Although, I guess you could create RESTful api as part of your service.


"I don't like MVC because that's not how the web works. Symfony2 is an HTTP framework; it is a Request/Response framework."

I disagree with this statement completely. MVC is definitely suited to the web if you look at it the right way.

1) The HTTP request is received by the Controller.

2) The Controller instantiates the Model (or Models) and activates the relevant method(s) on the Model(s) which each return a result which will usually be an array of data.

3) When the Model(s) have finished the Controller instantiates the View, inserts the data from the Model(s) then activates the method which transforms the data into HTML.

4) The Controller takes the result from the View and sends it back to the client as the HTTP response.

Just because MVC was invented for desktop applications years before the web existed and is therefore irrelevant for the web is a mistake made by people who cannot see the wood for the trees, who cannot see the big picture without fixating on irrelevant details. Each component in MVC has a distinct set of responsibilities, and provided that you create three components each of which fulfils one of these responsibilities - where the physical implementation is irrelevant - then you have MVC whether you like it or not.


I don't know Symfony but I already use other MVC frameworks (grails, codeigniter).

Models (Entities) represents the data and it is possible to define directly in the models some limits used later for the validation. For example, you can define for each attribute if it is required, its length, its pattern, ...

Services is maybe more symfony dependent. Comparing with Grails, Services are the components where you put your business code. In Java EE, it is the Beans. Note that a service can become a web service but it is not mandatory. A Service can also be called by the Controller in order to do some calculation before rendering the view.

I hope that my answer can help.