Php getUrlRules-切换到控制器
我有一个SearchModule.php具有以下功能:Php getUrlRules-切换到控制器,php,url-routing,yii1.x,Php,Url Routing,Yii1.x,我有一个SearchModule.php具有以下功能: class SearchModule extends CWebModule { // function init() { } /** * @return array Правила роутинга для текущего модуля */ function getUrlRules() { $customController = (Yii::app()->
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',
);
}
}
class SearchModule扩展了CWebModule
{
//函数init(){}
/**
*@返回阵列
*/
函数getUrlRules()
{
$customController=(Yii::app()->theme->getName()=='test'?'test':'');
返回数组(
$this->id.//'=>$this->id.//'.$customController.//',
$this->id.//'=>$this->id.//'.$customController.//',
$this->id./visas'=>$this->id./visas/fullVisasInfo',
);
}
}
我想弄清楚的是,如果我的主题设置为“测试”,如何使用另一个控制器。现在它有了名为HotelsController或LocationsController的搜索控制器。我试图实现的是,如果主题名称设置为“test”,它应该将所有请求从同一URL路由到TestHotelsController或TestLocationsController(/search/hotels应该路由到TestHotelsController而不是HotelsController)
我曾尝试在路由表的第二部分添加“Test”,但似乎没有任何效果。您没有将关键字
与任何类型的控制器名称组合。您可以为它指定一个自定义的唯一控制器名称,或者使用
关键字来读取给定的控制器。并且您的控制器名称不是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;
}
函数getUrlRules()
{
$customController=(Yii::app()->theme->getName()=='test'?'hotelsTest':'hotels');
if(strpos(Yii::app()->urlManager->parseUrl(Yii::app()->请求),“hotel”)){
$rules=数组(
$this->id'//'=>$this->id'/'.$customController'/',
$this->id.'//'=>$this->id.'/'..$customController.'/',
$this->id./visas'=>$this->id./visas/fullVisasInfo',
);
}
否则{
$rules=数组(
$this->id.//'=>$this->id.//',
$this->id.//'=>$this->id.//',
$this->id./visas'=>$this->id./visas/fullVisasInfo',
);
}
返回$rules;
}
我通过使用setControllerPath找到了一种方法,如下所示:
$customController = (Yii::app()->theme->getName() == 'test' ? 'test' : '');
$this->setControllerPath(__DIR__ ."/controllers/$customController");
在模块的init()函数中。通过这种方式,自定义控制器的名称保持不变,只更改了它的目录。这似乎不起作用,因为它不调用TestController的操作。我可以直接给他们打电话,但是通过这种路由,它会一直给标准的HotelsController打电话。我稍微修改了函数和控制器名称,当我直接调用/search/hotelsTest时,它会正确地调用测试控制器的操作,但是它不会根据需要切换到调用它。