Where should number_format (presentation logic) go without CakePHP?

After discussion with a colleague, we are mixed with the fact that, in our opinion, the logic related to opinion should go.

For example, let's say we have a number that we want to show in our opinion. I think that number_format (or CakeNumber::format , as we use CakePHP) should appear in the view as it is related to what we are showing. My colleague believes that he should go to the controller, because where all the logic goes.

In this case, we have two views: one for the website and end users, and an API view that returns a json response. If I were to put the code in the controller, I would need to check what kind I use, so as not to give a line in the json response when it really is an integer. For this reason, I strongly support the code in the view.

The question is, who is β€œright,” where is the formatting of numbers?

In addition to my argument about placing code in a view, he gladly uses htmlentities in the view, but I argue that if I am not allowed to number_format, it cannot have htmlentities and that it must be done in the controller.

+6
source share
4 answers

In accordance with the rules of MVC, all the logic should go in the model, and not in the controller. The controller only needs to pull out everything that is needed for viewing, and then transfer it to the view that will be displayed.

Which, in my experience, performance often ends in small pieces of logic. Converting number formatting or escaping content using htmlentities is all very little material at the end and is great for viewing, some people will even think about using these functions as formatting rather than business logic. A lot of people, including me, relate to small functions, such as these as helpers (as indicated by Armag in the comments), and using them in looks is accepted and definitely accepted.

This, of course, is all my opinion, formed over the years, trying to keep the content separate and in the right place, your mileage may change.

+4
source

For example, let's say we have a number that we want to show in our opinion. I think that the format number_format (or CakeNumber :: how we use CakePHP) should be displayed in the view, as this is due to what we are showing. My colleague believes that he should go to the controller, because where all the logic goes.

Your interlocutor is mistaken. You should use NumberHelper , which is a wrapper around the static CakeNumber. Usually you want to avoid static and singletones in your application, and the helper can also be an alias, so use the NumberHelper functions in the views. The view is the right place.

In this case, we have two views: one for the website and end users, and an API view that returns a json response. If I were to put the code in the controller, I would need to check what kind I use, so as not to give a line in the json response when it really is an integer. For this reason, I strongly support the code in the view.

The controller does not need to change one line to support json and another output. Well, you can use the built-in serialization of vars views for json / xml or create custom json views. Read this chapter. However, there is no need to create two methods or populate the controller with business logic, which is actually incorrect.

The question is, who is β€œright,” where is the formatting of numbers?

Definitely a view.

The controller should be skinny and not contain any business logic:

enter image description here

In addition to my argument about placing code in a view, he is happy to use htmlentities in the view, but I argue that if I am not allowed to number_format, it cannot have htmlentities and that this should be done in the controller.

Use h () , which is a shortcut for htmlspecialchars (). And make sure that you use it for everything that you echo there. All the main helpers that come with the frame will do this internally for you if you pass something through them, but pay attention to third-party helpers who may not use it!

+5
source

My colleagues should go to the controller, because where all the logic goes.

The controller does not include business logic. This is not MVC. This is atrocity.

In such cases, always imagine a scenario in which MVC was primarily intended to: replace your controller / view for an alternate interface. For example, imagine you are writing a command line client for your application, for example. to perform administrative tasks. You will want to do the same things on the command line that may be on your site, for example:

 $ admin users create Bob bob@example.com 

This does the same in the model as regular user registration on a website, but it requires a different controller (which can handle interaction with the CLI) and a different view (which simply displays plain text). Since you obviously do not want to repeat any business logic when writing this article, business logic is not part of the controller . Everything should be in the model.

And the formatting numbers are obviously responsible for the presentation, because it is a visual, human-readable problem.

+1
source

If data is presented for data management, it must be in the layer between the controller and the view. Your controller should retrieve data from the models, and your view should display that data. Your look can be anything: HTML, PDF, CSV, JSON, etc.

Look at the presenters. They take the essence and prepare their data for presentation in the presentation. Thus, your presenter class may have a method called priceFormatted() that returns the formatting of the price value with decimal places, thousands separator, currency characters, etc. That way, you can use the presenter methods in your HTML representation, but for other kinds (i.e. XML or JSON), you can just use the original value.

+1
source

Source: https://habr.com/ru/post/990023/


All Articles