Reading Time: < 1 minute

One of our legacy applications is using Zend framework 1.12. The requirement was to change the layout if one flag value is true in the request URI. We have a default layout enabled which was loading for every controller that belongs to the module. The challenge was to change the layout which needed only if the flag value is set to true. I struggled a little bit to find the logic to change the layout, so I am writing this post for my future reference.

Here is the code:

public function yourAction() {
    $getParams = $this->_request->getParams();

    if(isset($getParams['flag']) && true === $getParams['flag'])
        $this->_helper->_layout->setLayout('custom-layout');
    }
    // code
}

You should have your layout inside app/layout/module/custom-layout.phtml. In my layout, I don’t want to load any content so my layout code looks like below.

custom-layout.phtml

<?php
/**
 * It does not need all the JS, CSS, and HTML for this layout page
 * Because this layout will be used for custom pages and it is created using a
 * HTML BUILDER Plugins.
 * The plugin has all the JS, CSS and it loads everything on its view file.
 * Putting all plugin content together can help to modify the plugin or would
 * be easy during plugin upgrade.
 */
echo $this->layout()->content;