S
S
Space2015-06-15 15:22:03
Yii
Space, 2015-06-15 15:22:03

Where is the best place to write conditions in YII2?

There are several questions about yii2, namely code design.
1. Is it nice and expedient to write conditions and loops in the model, and not in the controller, if it is more convenient?
2. I need to perform ajax requests from the database. Should this be done directly in the controller, or is it equivalent to putting ajax into a view and passing it to the controller via POST?
3. What is the best way - to do all the loops with arrays in the controller and already transfer the variables to the view, or is it equivalent to disassembling the arrays in the view?
Are there any rules at all about this - code formatting?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dave, 2015-06-15
@ruslite

1. Is it nice and expedient to write conditions and loops in the model, and not in the controller, if it is more convenient?
Yes, it is desirable to write everything related to the processing of business logic (there are all kinds of parsing) in the model. In the controller, you need to call it and just pass the variables ($_POST, $_GET)
But the URL from the Ajax request still calls the controller action. POST'om should be performed only that changes something - writes to the database for example. And for selection it is necessary to use GET. In the view, the Klint logic is set, which calls the url and something is done in response - usually this is done in 99% of cases.
In no case should you parse arrays in a view. In view, you just need to display these prepared arrays, and they should be prepared in the model and, if necessary, change their contents through actions.
Read what is SOLID, Separation of Concerns
As for MVC:
- the models should be thick (all processing should be only there)
- the controllers should be thin (they should only be able to receive and transmit)
- and the view should be stupid (they should only know how to display)
Something like this

A
amstr1k, 2015-06-15
@amstr1k

It all depends on which approach you choose. You can generally select a layer of services that will do this or that work. But I would use views only to display data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question