Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models in Symfony2, and other MVC frameworks?

I'm trying to understand how Models work in proper MVC.

As far as I know, Models in MVC is where the application logic happens, Models are meat, or back bone of MVC. Views are just presentation, and Controllers are "glue" that asks Models to do some actions, return some data, and pass that information to the View that is presented to the user.

Now, I'm exploring all kinds of different MVC frameworks and would like to understand how to use models in MVC. Symfony 2 is interesting framework as far as Models goes, since there are no Models :)

I have problems grasping some of the features of Symfony2, and where does Models fit in Symfony2 MVC.

By definition, Models are where domain logic, and database actions goes.

So my questions are:

  1. In Symfony2 we have Entities, and Services, are those two Models in Symfony?
  2. What is the difference in Symfony2 Services, and Web Services?

So my questions are where is the Model in Symfony2? Since Model is a layer, composed of Domain Objects, and Data Mappers, then I can assume that Entities are Domain Objects, and Doctrine is Data Mapper, is that correct?

And where do Symfony2 services fit in?

like image 575
otporan Avatar asked Oct 29 '12 11:10

otporan


People also ask

Is Symfony an MVC framework?

Symfony is an MVC framework written in PHP. Its structure is designed to get the best out of the MVC pattern, whilst maintaining great ease of use. Thanks to its versatility and configurability, symfony is suitable for all web application projects.

What is PHP MVC framework?

What is PHP MVC framework? PHP MVC is an application design pattern that separates the application data and business logic (model) from the presentation (view). MVC stands for Model, View & Controller. The controller mediates between the models and views. Think of the MVC design pattern as a car and the driver.

What is the difference between Laravel and Symfony?

At a glance, there's one main difference between Symfony and Laravel which is that Symfony is both an Application Framework and a set of reusable components, while Laravel is simply a Framework. In fact, Laravel uses quite a few of Symfony's components.

What is a controller in Symfony?

In Symfony, a controller is usually a class method which is used to accept requests, and return a Response object. When mapped with a URL, a controller becomes accessible and its response can be viewed. To facilitate the development of controllers, Symfony provides an AbstractController .


2 Answers

  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.

like image 179
Hyunmin Kim Avatar answered Oct 24 '22 09:10

Hyunmin Kim


"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.

like image 41
Tony Marston Avatar answered Oct 24 '22 07:10

Tony Marston