2014-11-22 73 views
0

我遵循此tut(副本this)爲創建命名參數的自定義規則。在我的規則數組中,我添加了2個上面的行來解析向前和向後Assortment[groupCategory]參數。通過urlManager規則美化網址只能用一種方法

'urlManager'=>array( 
     'showScriptName'=>false, 
     'urlFormat'=>'path', 
     'rules'=>array(  
      'assortment/<Assortment[groupCategory]:\d+>'=> 'assortment/index', 
      'assortment/<Assortment%5BgroupCategory%5D:\d+>'=> 'assortment/index', 
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', 
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
     ), 
     ), 

這工作正向: 與http://tarex.ru/assortment/index/Assortment[groupCategory]/1的YII識別Assortment[groupCategory]作爲GET參數等於1

但是,如果從表單我請求

  • http://tarex.ru/assortment/index?Assortment[groupCategory]=2
  • http://tarex.ru/assortment/index?Assortment%5BgroupCategory%5D=2

它不會將其轉換爲人類可讀的ULR,像這樣:

http://tarex.ru/assortment/index/Assortment[groupCategory]/2

爲什麼?嘖嘖,這是雙向網址管理器

另一方面,當使用路徑發佈/索引和參數標籤創建URL時,urlManager組件也將使用此規則生成所需的URL /index.php/posts/yii。出於這個原因,我們說urlManager是一個雙向URL管理器。

回答

1

它不會將其轉換爲人類可讀的ULR

是的,Yii的不變換網址在瀏覽器中地址欄,也沒有在形式的行動參數。一切都在「幕後」運行。

我建議你到你的規則

'assortment/<Assortment[groupCategory]:\d+>'=> 'assortment/index' 

改寫爲

'assortment/<groupCategory:\d+>'=> 'assortment/index' 

這樣,如果你去網址http://tarex.ru/assortment/index/1actionIndex()方法將在名爲AssortmentController控制器被調用。並將參數$groupCategory = 1傳遞給它。爲了處理傳遞的變量,你可能需要簽名更改方法:

echo Yii::app()->controller->createUrl('assortment/index', array('groupCategory' => 1)) ; 

一個url /assortment/index/1絕:

public function actionIndex($groupCategory) {} 

「返回方式:」如果你以這種方式獲得的參數創建網址會被創建。

+0

我可以使用'Assortment [groupCategory]',複合一,作爲參數而不是'groupCategory'嗎? – 2014-11-22 09:53:35

+0

恐怕這個'assortment [groupCategory]'只能「單向」工作。因爲將值分配給您分配給數組成員的'$ Assortment [groupCategory]'時。你知道,括號不能用於PHP變量名。什麼是使用它們的原因? – 2014-11-22 10:06:43

+0

或者你可以試試'echo Yii :: app() - > controller-> createUrl('assortment/index',array('Assortment [groupCategory]'=> 1));' :) – 2014-11-22 10:07:26