roxik2016-09-19 19:50:24
roxik, 2016-09-19 19:50:24

A couple of questions about MVC. Can you help?

Wrote my mini-framework. So. The site has a choice of currency, there will be 2 variables with types (wmr, wmz, wme) and names (rubles, dollars, euros) where the types are used in the model, and the name in the visualization. How to store them correctly and where?
One more question. There are some parts of the site that are repeated on many pages of the site, and their content is taken from the database. To request them every time in the controller? those. duplicate code in model and controller? what do you advise?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Sergey, 2016-09-20
Protko @Fesor

what do you advise?

1) forget about MVC for a minute. While we will talk about the "model". Also, forget about the presentation, HTML, etc. for now. Only PHP, only hardcore.
2) move all actions into separate classes so that you have one object per action
3) move all duplication into separate objects (this is to specify what is meant by the second paragraph) and move everything depending
4) since you have a lot objects - use the dependency container
5) to work with the database, it makes sense to organize a DAO for example. That is, so that everything related to SQL would be somehow hidden in them and not spread over all objects. You can also read about Table Data Gateway.
That is, we must form some kind of API. We need news - we get an object that can get them. Something else is needed - for this something there is also an object that can do it. How he will do it is another matter.
Next, we need to add a layer that will convert the data returned by the previous API into the format we want. These will be controllers in fact. They need a router, template engine, etc.
In order to eliminate UI duplications, it makes sense to single out separate components (for example, a block with the latest news is a separate UI component). That is, the page will be assembled as if from pieces.
Well, something like this.

dev400, 2016-09-19

in the model you implement the selection, in the controller you call the model and pass it to the template. In the template, display data through a loop.
The main page controller (for example) receives a request, and pulls the widget. The widget pulls the database, which returns an array to it from mysql. This array is passed from the model to the widget template. Next, the widget returns an already generated view to the main page controller, which the main page controller passes to the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question