1

I would love to be sure, if it is not a ugly practice to do the following.

In my controller I render a block template layout:

    public function saveactionAction() {
        $this->loadLayout();
        $this->rendereLayout();
    }

Render some simple HTML form for saving:

    <form action="" method="POST">
        <input name="somevar" type="text">
        <input type="submit">
    </form>

and in my block's template, I receive a form data, filter it and save it in the constructor:

 class MyModule_MyCompany_Block_SaveAction extends Mage_Core_Block_Template {

        public function __construct() {
            if($params = $this->getRequest()->getParams()){

                // custom filtering and saving to data database
            }
            parent::__constuct();
        }
    }

The question is: is it normal or should I do the logic inside the controller preferably?

  • 1
    Do you understand the difference between blocks classes and templates? – Jonathan Hussey Nov 30 '15 at 15:33
  • I thought that I do, but have doubts about it: block class define logic, and template render html according to this logic ? – Ilja Nov 30 '15 at 15:55
  • 3
    Well a block class runs an include against any template file you define, and this allows you to call methods defined in that block class from within the template using $this->someMethod(). So generally you would define your block and template in your layout file for the correct layout handle, then build the block and template with HTML and method calls in your template, and the methods in your block. – Jonathan Hussey Nov 30 '15 at 16:05
1

The short answer is "no" but I will provide some arguments.

The block script is to define everything that is linked with the display of the block. Filtering can be one action that can be done in there.

The controller makes the link between view and models. The assertion that you can do custom filtering and saving to data database is half false as you can do the filtering in controller but all the logic that involve database should be done with the help of models (both basic and resource ones). The controller should only call these models. Moreover, prefer the functions like indexAction() to do the logic than the constructor in controller and elsewhere.

The basic model has a large amount of tools that permits to retrieve easily data from the database without calling resource's models.

The resource and collections permit to retrieve data from database.

  • So, if I correctly understood, I can just trigger some block's methods from controller? like i have filtering method in block and just make $block->myFilteringMethod($params); ? – Ilja Nov 30 '15 at 16:20
  • 1
    the comment jonathan let is good example on how should be done the display logic within magento. you should filter in the block the data passed by the controller not call block from the controller. at all, limit the block updates inside controller and prefer layout. the method in block class are (normaly) only for display purpose – Christophe Ferreboeuf Nov 30 '15 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.