3

I have a SearchModule.php has the following:

class SearchModule extends CWebModule
{    
    // function init() { }
    /**
     * @return array Правила роутинга для текущего модуля
     */
    function getUrlRules()
    {
        $customController = (Yii::app()->theme->getName() == 'test' ? 'Test' : '') . '<controller>';

        return array(
            $this->id.'/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>' => $this->id.'/' . $customController . '/<action>',
            $this->id.'/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>/<cityId:\d+>' => $this->id.'/' . $customController . '/<action>',
            $this->id.'/visas' => $this->id.'/visas/fullVisasInfo',
        );
    }
}

What I'm trying to figure out is how to use another controller IF my theme' set to 'test'. Right now it has search controllers named like HotelsController or LocationsController. What I'm trying to achieve is if theme' name set to "test", it should route all requests to TestHotelsController or TestLocationsController from the SAME URL (/search/hotels should route to TestHotelsController instead of HotelsController).

I have tried doing it by appending 'Test' to the second part of routing table, but that didn't seem to do anything.

Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39

2 Answers2

2

You don't combine the keyword <controller> with any kind of controller name. You either give it a custom unique controller name, or the <controller> keyword to read the given controller. And your controller name is not TestController, but it's TestHotelsController, so, try to change your code like this:

function getUrlRules()
{
    $customController = (Yii::app()->theme->getName() == 'test' ? 'hotelsTest' : 'hotels');

    if(strpos(Yii::app()->urlManager->parseUrl(Yii::app()->request), 'hotel')) {
        $rules = array(
            $this->id . '/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>' => $this->id . '/' . $customController . '/<action>',
            $this->id . '/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>/<cityId:\d+>' => $this->id . '/' . $customController . '/<action>',
            $this->id . '/visas' => $this->id . '/visas/fullVisasInfo',
        );
    }
    else {
        $rules = array(
            $this->id.'/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>' => $this->id.'/<controller>/<action>',
            $this->id.'/<controller:\w+>/<action:(SupportBlock)>/<countryId:\d+>/<cityId:\d+>' => $this->id.'/<controller>/<action>',
            $this->id.'/visas' => $this->id.'/visas/fullVisasInfo',
        );
    }

    return $rules;
}
Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • That doesn't seem to do the trick as it does not call TestController' actions. I can call them directly, however with this routing it keeps calling standard HotelsController. I have modified the function and controller name slightly, and when I directly call /search/hotelsTest it calls the action of the test controller properly, HOWEVER it does not switch to calling it as needed. – Daniel Protopopov Dec 08 '16 at 06:30
2

I have found a way to this by using setControllerPath like so:

$customController = (Yii::app()->theme->getName() == 'test' ? 'test' : '');
$this->setControllerPath(__DIR__ ."/controllers/$customController");

in the init() function of the module. This way the name of custom controller remains the same, only the directory for it changes.

Daniel Protopopov
  • 6,778
  • 3
  • 23
  • 39