2

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.

mae
  • 14,947
  • 8
  • 32
  • 47
  • Well, I don't know anything about aliases in controllers but you can use 'forward()' to use another controller's action. http://www.yiiframework.com/doc/api/1.1/CController#forward-detail. You can just have the actions named in your controller and forward to the other controller actions. The forward will not be a redirect but a reuse. – JorgeeFG Apr 17 '13 at 12:53
  • @Jorge This seems to be a shot in the right direction. But is it possible to forward all actions without manually specifying each one? – mae Apr 17 '13 at 12:58
  • 1
    What do you want to achieve? It's never neccessary to create two identical controllers. – Michael Härtl Apr 17 '13 at 13:00

2 Answers2

5

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

Michael Härtl
  • 8,428
  • 5
  • 35
  • 62
0

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();
    }
}
Soyale
  • 305
  • 1
  • 6