0

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>',
            ),
        ),
        ...
    ),
);
automatix
  • 14,018
  • 26
  • 105
  • 230
  • The `'url' => 'Yii::app()->createUrl("foo", array("id" => $data->myid))'` generates `/index.php/foo/id/123`, not `/index.php/foo/myid/123`. Are you sure everything in your question is correct? – hamed Jun 20 '15 at 03:32
  • Thank you for the hint. Yes, I mistyped. I corrected the question now. Do you have an idea, how to solve the problem? – automatix Jun 20 '15 at 07:07

2 Answers2

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

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));
    }
Double H
  • 4,120
  • 1
  • 15
  • 26
0

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)
{
    ...
}
hamed
  • 7,939
  • 15
  • 60
  • 114
  • Yes, it's correct. But it's the next step. I've just tried to change the name of the argument in the controller action -- no effect: the generated URL looks then like `/index.php/bar/id/123`. Since it has nothing to do with the URL creation. It's only important on the next step of the URL matching. So I want first to get the URL created. – automatix Jun 20 '15 at 08:12