Search code examples
phpcakephpseparation-of-concerns

Where to put certain logic in CakePHP


I've recently started to rewrite a project I did a few years ago using CakePHP. I'm trying to do everything 'right' this time, so maybe someone get give me a a pointer on doing to the following:

I'm showing a simple table from a table using Model->find('all') in the View. There are two boolean fields in this table, that together make up something I need to show to a user. So: 0x0 = 'A', 1x0 = 'B', 0x1 = 'C', 1x1 = 'D'. Where should I put this logic? I've been thinking about the following methods:

  1. The view
  2. A View helper
  3. The controller
  4. Something in the Model so that Model->find('all') outputs this value (is that even possible?)

This task might seem trivial, but I think it might learn me getting this project organized and maintainable from the start.

Thanks!


Solution

  • Well, it depends on the type of logic for making up final table (is it presentation or business?).

    Imagine you add new type of UI, for example command line interface. How would you show your table there? The data passed to View has to be same for both HTML and console presentations. So the logic which is responsible for preparing that data - is business logic and it should be placed in Model. The logic responsible for displaying the data should be placed in View (maybe in view helper if it's used more than once).

    And never place this kind of logic in Controller.