2

Currently, the model layer's service section holds MainService with the following objectives

MainService => Communicates with persistence layer,
               does the UI logic,
               renders the respective view

The OP of the question here describes the idea; views should do the UI and then render (depends) the respective template.

An example of MainService is similar to

echo $this->factory->template()
    ->file('/path/to/template')
    ->set('url', 'some/url')
    ->render();

It is clear that this contradicts clearly with the concept of Views. And this is where I get confused - the current implementation of service looks a lot like a view. Is it a view then?

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56
  • That definition seem to use "service" for what I would consider a controller. If you look at how some large "mvc" frameworks (symfony, laravel) have implemented mvc then you route a request to a controller which takes the user input and does whatever it should do with it, preferably using services. And then render a view/template – JimL May 19 '16 at 20:57
  • Afaik, the controller in php is mostly a service, which is a part of the model layer. – sitilge May 19 '16 at 20:59
  • In the mentionef "mvc" implementations they use services like a log service, mail service, etc. Note that I'm constantly using "mvc" with quotes as it's only to a certain level that this pattern is implemented. It seems to be not set in stone and often somewhat opinionated how this (and other) patterns get implemented. if you're interested in a debate on the structure/pattern I suggest you post at http://codereview.stackexchange.com/ – JimL May 19 '16 at 21:04
  • Yes, they use the buzzword "mvc" for marketing purposes. Also, the OP of the post mentioned has some good quotes on this. – sitilge May 19 '16 at 21:14
  • @JimL fyi, another article here: https://r.je/views-are-not-templates.html – sitilge May 21 '16 at 14:01
  • Yup, but in your code you're only telling something to render a template. That render function may do stuff that Id consider part of the view, and the part calling it (MainService) is still (imo) the controller :) – JimL May 21 '16 at 14:07

1 Answers1

-1

The Controller is responsible for:

  • reading user input

  • requesting data from the Model

  • calling the View with that data

The Model is responsible for:

  • retrieving data from persistence layer

  • shaping the data as requested by the Controller

The View is responsible for:

  • rendering the template with the data provided by the Controller and showing it to the user

In your example, it is correct for MainService to communicate with persistence layer. But, doing UI logic and rendering the view (template) is a job for the View.

If you use a framework like Laravel, Zend, Angular2, you will never see the View layer. You will define the template, but the framework does the rendering based on the data you provide in the Controller.

Example flow with Laravel for displaying a user's profile page:

  • Laravel will call a Controller action based on route settings (certain URL to a certain Controller and action)

  • Controller will read parameter (user id)

  • Controller will request from UserService the User with that id

  • The service will make a request to the database and build a User model (object) that will return to Controller

  • The Controller will call the View with a template identifier and an array of data taken from the User object

  • The View will render the template with data from the array

Mihai Răducanu
  • 12,225
  • 4
  • 22
  • 31