Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Magento what is the correct data flow from POST to controller to model to block to template?
    primarykey
    data
    text
    <p>When data is sent to a Magento extension via GET or POST, what is the correct way to use the MVC to validate the data, process it based on some business logic then output the results to the screen?</p> <p>As I understand it, the controller is the correct place to receive and validate the data being submitted (perhaps calling on a model to do the actual validating). Models are the correct place for any business logic, taking the request parameters and processing them. The block is the correct place to prepare data for output, which it can request from a model.</p> <p>I understand how a controller can receive posted data and forward it to a model</p> <pre><code>class Company_Project_IndexController extends Mage_Core_Controller_Front_Action { public function receivePostedData() { $model = Mage::getModel('project/somemodel'); if($model-&gt;validateData( $this-&gt;getRequest()-&gt;getPost('post_vars') )) { $model-&gt;processData( $this-&gt;getRequest()-&gt;getPost('post_vars') ); } } } </code></pre> <p>which the model will then process</p> <pre><code>class Company_Project_Model_Somemodel extends Mage_Core_Model_Abstract { public function validate( $data ) { //return true of false if data is valid/invalid } public function processData( $data ) { //Do something with the data } public function getData() { //return something } } </code></pre> <p>I also understand how a block can instantiate a model and get data from it</p> <pre><code>class Company_Project_Block_Display extends Mage_Core_Block_Template { public function getData() { $model = Mage::getModel('project/somemodel'); return $model-&gt;getData(); } } </code></pre> <p>The problem I have is the disconnect between the controller and the model working together and the Block and the model working together. If the controller is pushing data into the model which is changing the models state, how do I then get data into the block from the model based on that changed state?</p> <p>As I see it, there are the following possible solutions:</p> <ul> <li>Use Mage::getSingleton to get my model so I'm referring to the same instance in the controller and the block</li> <li>Refer to the post data directly inside the model or the block</li> <li>Use the registry to store some of the model state</li> </ul> <p>I've know idea if any of the above are correct or which one I should be using.</p> <p>I've taken a look at the core Catalogue Search module as its doing exactly what I need, but I got completely lost in the code.</p> <p>I know that several other very similar questions to this have been asked and answered but I'm still in the dark.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload