0

I need to send a relationship status between two users (which can has values like 'friends', 'undefined', 'ignore') from a Model into a View.

Inside a template I need to create a links and labels for each case, something like:

switch ($status) {
        case 'friends': 'label' = 'Unfriend', 'link' = 'user_controller/unfriend/'.$id;
        case 'undefined': 'label' = 'Add to friends', 'link' = 'user_controller/addfriend/'.$id;
     ...
}

So where should I do this switch - inside a Model and then return array with labels and links, or just return a string and make stuff inside a View. Which way is better?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    The sooner you realize that what you're doing in PHP isn't _actually_ the MVC pattern the better. In the actual MVC pattern, the view listens to the model for changes through an observer pattern - this is quite ineffective in the PHP case since HTTP is a stateless protocol and your views can't listen from the client to the server for updates. Typically, what serverside frameworks do is pass a "model" (or "view model") object to the view which it uses to perform the templating. In the model approach the view can indeed decide this logic (what controller to link to), in the vm one the vm does it – Benjamin Gruenbaum May 02 '15 at 11:01
  • 1
    It's also definitely possible to do this logic in your controller - don't worry about what the design pattern or architectural pattern you're using is called - worry about writing clean and maintainable code where communication between different components is clear and who does what is obvious to a first time reader. – Benjamin Gruenbaum May 02 '15 at 11:04
  • sure, i understand about differences between classic/desktop mvc and web. i'm also trying to make controllers as thin as possible – avada kedavra May 02 '15 at 11:11

1 Answers1

0

Yes, I would go for the Model Layer and leave the View just as Template (since using MVP, MVC-inspired). Quoting a great article:

In proper MVC adaptation, the M contains all the domain business logic and the Model Layer is mostly made from three types of - Domain Objects (business logic), Data Mappers (storage) and Services (the bridge communication between other parts)

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56