Search code examples
magentocontrollermagento-1.6

Magento - is there a default method for controller classes?


I've got a controller file that's getting a bit repetitive, with several actions that all do the same thing, load and then render the layout. Because everything I need is defined in a block of custom layout XML I don't actually need the controller to do anything else. The way I've written it feels like I'm duplicating code over multiple methods. Is there one "default" method (like defaultAction) that I can call instead?

<?php
class Markie_Module_LiteratureController extends Mage_Core_Controller_Front_Action {

    public function catalogsAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function postersAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function helpAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}

Solution

  • Thanks to @fantasticrice for pointing out this answer: https://stackoverflow.com/a/22252395/2036972

    I've changed my code to the following and it's working really well. Lots shorter and no code duplication. I'm going to leave this question open for the weekend to see if anyone has a better way of doing it, or has issues with this answer.

    <?php
    class Markie_Module_LiteratureController extends Mage_Core_Controller_Front_Action {
    
        public function __call()
        {
            $this->loadLayout();
            $this->renderLayout();
        }
    }