D
D
Deno952016-02-14 13:20:55
symfony
Deno95, 2016-02-14 13:20:55

When do you need to create a controller in Symfony?

Is it correct in MVC to create a controller for each page?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Natarov, 2016-02-14
@Deno95

The controller only describes actions on what you need. And it gets data from models via getPropertyObject queries and sets them if you create something new. setPropertyObject. The processing of data correctness itself takes place in the models, as well as the description of the methods of getters and setters. Also in other frameworks there are scripts, AcessControl - access control.
An example of the User model: what controller should it have?
index - what will happen if a person visits the profile page at site.com/user/index/{id of the desired user}
registration - how a new user is created, the form is described inside it.
Login - how the user is authorized, do not confuse with authentication.
Logout - how the user logs out
update - how the user changes his profile
delete - what happens if the user is deleted.
The essence of what is happening: I will try to be very trivial, but I hope you re-read the principle of MVC. A person visits the site at the address, for example, site.com/user/registration
Your User control fires, then comes the registration action (action inside the controller) registration Inside this action, there are two variations of the "page", when the user is not logged in, we show the form, when the user is logged in, there should be no access to the registration function. In the case of a form, the controller generates a form view and waits for the user to fill it out. By clicking on the Submit button, validation is called (the method call itself is written in the controller, but what exactly this method does is written in the model). An instance of the User class is created and filled with data from the form, validated if the validation is successful. it saves to the database and generates a view (page) about success.
As for your "pages" as you can see on one action, there can be many pages and different variations and they do not depend on controllers. Most often, of course, one type for each action, but there are exceptions.
Recommended for viewing:
Here is a self-written MVC, but it is very simple and describes the main idea of ​​​​this concept, after viewing, you can write your own MVC, but you should not do this. There are enough crutches on the Internet. Just take a look and clarify for yourself some points.
Part 1
Part 2

D
Dmitry, 2016-02-14
@DP-Studio

For every model. What's with the pages? Well, plus there are also service controllers (for example, processing image collections, etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question