How Can I Get A Dynamic $Layout For Sitecontroller Based On Which Controller It Was Before

Hi guys,

I have a problem. I have an ACL in controller LTEExecController, which goes back to SiteController, when it denies a user. But there the $layout variable points already to a view (hardcoded) with its navigation menu.

I need $layout to be changed based on the Controller that it came from.

How can I do that without being hacky? I don’t want $layout to be hardcoded. I want it to be dynamic.

simple solution would be a beforeAction in your Controller.php




<?php

# ...

   public function beforeAction($action)

    {

        switch ($this->id)

        {

            case 'site':

                $this->layout = "//layouts/column1";

                break;

            case 'posts':

                $this->layout = "//layouts/column3";

                break;

            default:

                $this->layout = "//layouts/superLayout";

                break;

        }


        return parent::beforeAction($action);

    }



In which controller?

I have only controller LteExecController and SiteController (plus the two parent controllers). When the access gets denied through the ACL, then it will instantiate the controller SiteController. How does your code help?

I’ve tried it out. It doesn’t work.

It works I have tested it. try component/controller.php

I’ve found out why it didn’t work. It’s a bug in the Yii version 1.1.11.

I’ve updated it to 1.1.15 and it worked. What a silly coincidence that leads to two different results.