V
V
Vladimir Borisyuk2018-04-23 12:52:36
Web development
Vladimir Borisyuk, 2018-04-23 12:52:36

What is the best way to display a view with data to the user using the PHP MVC template?

The task is to display to the user a view with data from the database using the MVC template.
I see two options.
1) The Controller generates a view, receives data from the model and passes an associative array to the view, then using php it displays the view with the data.
2) The Controller generates a view without data, then, when the DOM is ready, an AJAX request is sent to another Controller method, which receives the corresponding data from the Model and returns data in json format, which are displayed on the view in the succes handler in ajax.
What implementation would you recommend?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2018-04-23
@By_Engine

The first, of course. In the second, there are too many body movements for the same result.

D
danforth, 2018-04-23
@danforth

Depends on how long the data is selected from the database. If it is less than ping to the server, then it is more reasonable to wait and transfer the already rendered template. If the data is selected for a long time, you can send the page to the user, and show some kind of loader, and then draw the data received via AJAX.
I even did something like this:
1. A request comes to the server, the response is given, say, 100 ms.
2. If during this time the answer did not come from the database, we give the user a page without data, and a URL with a hash where to come for the data.
3. At this time, the database has become inflated, and we put the incoming response into the cache, since we have not had time to jump onto the page.
4. On the client, in the meantime, we requested our URL with a hash, and we are waiting for a response from the server, as soon as they appear in the cache, the server immediately sends a response.
I did it for fun, and not in PHP. This is more difficult to implement in PHP.

M
mindgrow, 2018-04-23
@mindgrow

The second option will work faster.
If you feel confident, then you can immediately implement this option.

Y
Yan-s, 2018-04-23
@Yan-s

Depends on the task. Both options can be used.
If your page is interactive, AJAX is used for the main content, then the second method will be correct. Otherwise you will have duplication in the page controller and in the AJAX controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question