I come from different MVC frameworks (like Symfony) in Magento. I read a lot about Magento best practices and I see that Magento does not use the typical MVC style. Alan Storm wrote the following:
it’s not the duty of the controller to set the variables for view [...] the controller’s task is to do certain things for the models and then inform the system about the rendering time of the layout.
I think I understand this approach, as it can provide flexibility for blocks.
Right But what about forms?
In a typical MVC structure, you will receive request parameters in the controller, check form data in the controller, do operations with the model (save, load, etc.) or redirect there if necessary, and when everything is clean and tidy, you will provide freshly baked weekends parts to view.
In Magento, all this should happen inside the block, and the (thin) controller should only prepare the layout and then render it. (If I understand.)
I tried to find an article (manual, forum topic, whatever) that describes the steps for creating a separate module with its own new model, which can be edited through a form in the user interface. I would like to see how a user form should work in an interface. I found only general articles about blocks, forms, changes or creating adminhtml forms or setting up contact registration forms or newsletters.
I have done it. Now it works, but I am not satisfied. So, I checked the source code of the Contact form in the main module, and they mixed up the whole picture for me. The built-in contact form uses the IndexController for most of the above operations (almost), like standard MVC.
Can someone suggest me a best practice on how to manage a simple thread like the following? (I have a solution for them below, but I'm not sure if this is the “correct Magento path”):
- When the page loads, show the form in the block, which is included in a separate page
- Load the model object from the database with the query parameter
- Filling in object data in the form
- When the user submits the form, processes the form data, validates them
- If the validation fails, display the form again and the error popup
- If OK, save the data in the database, show the page with gratitude
My confusion is mostly around:
- Where can I get and manage the request parameter? (I did this in a block class file)
- And load an object from the database based on this? (Also, and then passed to phtml)
- How do I pass it to a view if I don't upload it? (I would know a way, but I don't know how best.)
- Where should form data (POST) be processed, validated and stored? (Block?)
- How to use redirects in a block? Is redirection necessary, since the gratefully page should be another block / page? Or just an alternative (conditional) view of the same block?