mae
mae

Reputation: 15656

Creating a simple controller alias

I'm not sure I am using the proper terminology, so I will describe what I want to achieve.

I have a controller called ControllerA and want a "virtual" controller called ControllerB to function exactly the same as ControllerA.

Basically I just want the url site.com/ControllerB to load up the same page as site.com/ControllerA (but not redirect).

Hope my description is clear enough.

Upvotes: 3

Views: 670

Answers (2)

Michael Härtl
Michael Härtl

Reputation: 8587

You can achieve what you want with a simple URL rule:

'controllerA/<a>'   => 'controllerA/<a>',
'controllerB/<a>'   => 'controllerA/<a>',

Read more about URL rules here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls

Upvotes: 6

Soyale
Soyale

Reputation: 305

You can extend ControllerA with ControllerB and provide extended controller name. Next override getViewPath method. Attribute extendedControler give us basic controller name.

class ControllerBController extends ControllerAController
{
    private $extendedControler = 'ControllerA';
    public function getViewPath() {
        $nI = Yii::app()->createController($this->extendedControler);
        return $nI[0]->getViewPath();
    }
}

Of course you can use some string modification. Like str_ireplace:

class Klient2Controller extends KlientController
{
    public function getViewPath() {
        //We must extract parent class views directory
        $c = get_parent_class($this);
        $c = str_ireplace('Controller', '', $c); //Extract only controller name
        $nI = Yii::app()->createController($c);
        return $nI[0]->getViewPath();
    }
}

Upvotes: 0

Related Questions