getUrlRules - 切换到控制器
getUrlRules - switch to controller
我有一个 SearchModule.php 有以下内容:
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',
);
}
}
我想弄清楚的是,如果我的主题设置为 'test',如何使用另一个控制器。现在它有名为 HotelsController 或 LocationsController 的搜索控制器。我想要实现的是,如果主题名称设置为 "test",它应该将所有请求从 SAME URL 路由到 TestHotelsController 或 TestLocationsController(/search/hotels 应该路由到 TestHotelsController 而不是 HotelsController ).
我尝试通过将 'Test' 附加到路由 table 的第二部分来实现,但这似乎没有任何作用。
您不要将关键字 <controller>
与任何类型的控制器名称结合使用。您可以给它一个自定义的唯一控制器名称,或者 <controller>
关键字来读取给定的控制器。而且您的控制器名称不是 TestController
,而是 TestHotelsController
,因此,请尝试像这样更改您的代码:
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;
}
我已经通过使用 setControllerPath 找到了解决此问题的方法,如下所示:
$customController = (Yii::app()->theme->getName() == 'test' ? 'test' : '');
$this->setControllerPath(__DIR__ ."/controllers/$customController");
在模块的init()函数中。这样自定义控制器的名称保持不变,只是它的目录发生了变化。
我有一个 SearchModule.php 有以下内容:
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',
);
}
}
我想弄清楚的是,如果我的主题设置为 'test',如何使用另一个控制器。现在它有名为 HotelsController 或 LocationsController 的搜索控制器。我想要实现的是,如果主题名称设置为 "test",它应该将所有请求从 SAME URL 路由到 TestHotelsController 或 TestLocationsController(/search/hotels 应该路由到 TestHotelsController 而不是 HotelsController ).
我尝试通过将 'Test' 附加到路由 table 的第二部分来实现,但这似乎没有任何作用。
您不要将关键字 <controller>
与任何类型的控制器名称结合使用。您可以给它一个自定义的唯一控制器名称,或者 <controller>
关键字来读取给定的控制器。而且您的控制器名称不是 TestController
,而是 TestHotelsController
,因此,请尝试像这样更改您的代码:
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;
}
我已经通过使用 setControllerPath 找到了解决此问题的方法,如下所示:
$customController = (Yii::app()->theme->getName() == 'test' ? 'test' : '');
$this->setControllerPath(__DIR__ ."/controllers/$customController");
在模块的init()函数中。这样自定义控制器的名称保持不变,只是它的目录发生了变化。