2015-06-19 12 views
0

我使用CGridView顯示的數據表:如何在Yii中創建沒有參數名稱的URL?

/application/protected/views/foo/bar.php

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'my-grid', 
    'dataProvider' => $dataProvider, 
    'filter' => $model, 
    'columns' => array(
     'myid', 
     ... 
     array(
      'class' => 'CButtonColumn', 
     ), 
    ), 
)); 

即創建的表有三通爲每一行:viewupdate,和delete;例如(對於view):/index.php/foo/123,其中123是元素的ID(或主鍵值)。

現在我想修改控件調用,以獲得不同的按鈕鏈接像/index.php/bar/123

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'my-grid', 
    'dataProvider' => $dataProvider, 
    'filter' => $model, 
    'columns' => array(
     'myid', 
     ... 
     array(
      'class' => 'CButtonColumn', 
      'template' => '{view}{update}{delete}', 
      'buttons' => array(
       'view' => array(
        'url' => 'Yii::app()->createUrl("bar", array("myid" => $data->myid))', 
       ) 
      ), 
     ), 
    ), 
)); 

的(視圖)鏈接我得到這個樣子的:/index.php/bar/myid/123 - 並在請求結束與404錯誤。

如何構建URL中沒有參數名稱的鏈接?


其他信息 - 在/application/protected/config/main.php我的路由配置:

return array(
    ... 
    'components'=>array(
     ... 
     'urlManager'=>array(
      'urlFormat'=>'path', 
      'rules'=>array(
       '<controller:\w+>/<id:\d+>'=>'<controller>/view', 
       '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', 
       '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
      ), 
     ), 
     ... 
    ), 
); 
+0

的' 'URL'=>「的Yii ::應用程序( - > createUrl( 「富」,陣列( 「ID」=> $數據 - >身份識別碼) )''生成'/ index.php/foo/id/123',而不是'/ index.php/foo/myid/123'。你確定你問題中的一切都是正確的嗎? – hamed

+0

謝謝你的提示。是的,我打錯了。我現在糾正了這個問題。你有一個想法,如何解決這個問題? – automatix

回答

0
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'my-grid', 
'dataProvider' => $dataProvider, 
'filter' => $model, 
'columns' => array(
    'myid', 
    ... 
    array(
     'class' => 'CButtonColumn', 
     'template' => '{view}{update}{delete}', 
     'buttons' => array(
      'view' => array(
       'url' => 'Yii::app()->createUrl("bar", array("myId" => $data->myid))', 
      ) 
     ), 
    ), 
), 
)); 

在/protected/config/main.php

'urlManager'=>array(
     'urlFormat'=>'path', 
     'showScriptName' => false, 
     'rules'=>array(
        '<controller:\w+>/<myId:\d+>'=>'<controller>/view', 
     ), 
    ), 

變化行動查看如

public function actionView($myId){ 
     $model= User::model()->findByPk($myId); 
     $this->render('view' , array('model' => $model)); 
    } 
+0

這正是我嘗試過的,不起作用的:'/ index.php/bar/id/1'。我想通過'myid'作爲參數,並得到像'/ index.php/bar/1'這樣的URL。 – automatix

+0

可能會在上面的代碼將幫助你。 –

0

當你有/index.php/bar/myid/123的URL,而BarControlleractionView必須myid(不id)作爲參數。可能你有actionView($id),所以這會導致問題。所以,你需要擁有的actionView這樣的:)

public function actionView($myid) 
{ 
    ... 
} 
+0

是的,這是正確的。但這是下一步。我剛剛嘗試在控制器操作中更改參數的名稱 - 無效:生成的URL看起來像'/ index.php/bar/id/123'。因爲它與URL創建無關。它只對URL匹配的下一步很重要。所以我想先獲取創建的網址。 – automatix

相關問題