How to create URLs without parameter names in Yii?

559 views Asked by At

I'm using the CGridView to display a data table:

/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',
        ),
    ),
));

That created a table with three links for each row: view, update, and delete; e.g. (for the view): /index.php/foo/123, where 123 is the ID (or primary key value) of the element.

Now I want to modify the widget call, in order to get different buttons links like /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))',
                )
            ),
        ),
    ),
));

The (view) link I'm getting looks like this: /index.php/bar/myid/123 -- and the request ends with a 404 error.

How to build links without the parameter name in the URL?


Additional info -- my routing configuration in the /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>',
            ),
        ),
        ...
    ),
);
2

There are 2 answers

2
Double H On
$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))',
            )
        ),
    ),
),
));

In /protected/config/main.php

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

Change Action View As

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

When you have /index.php/bar/myid/123 as url, the actionView of BarController must have myid (not id) as parameter. Probably you have actionView($id) and so this cause the problem. So you need to have actionView like this:

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