【问题标题】:How to create URLs without parameter names in Yii?如何在 Yii 中创建没有参数名称的 URL?
【发布时间】:2015-09-05 22:56:06
【问题描述】:

我正在使用CGridView 来显示数据表:

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

这创建了一个表,每行包含三个链接:viewupdatedelete;例如(对于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>',
            ),
        ),
        ...
    ),
);

【问题讨论】:

  • 'url' =&gt; 'Yii::app()-&gt;createUrl("foo", array("id" =&gt; $data-&gt;myid))' 生成 /index.php/foo/id/123,而不是 /index.php/foo/myid/123。您确定问题中的所有内容都正确吗?
  • 感谢您的提示。是的,我打错了。我现在纠正了这个问题。你有什么想法,如何解决这个问题?

标签: php url yii url-parameters yii-routing


【解决方案1】:
$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));
    }

【讨论】:

  • 这正是我尝试过的,但没有成功:/index.php/bar/id/1。我想将myid 作为参数传递并获取类似/index.php/bar/1 的URL。
  • May be Above Code 会帮助你。
【解决方案2】:

当您有/index.php/bar/myid/123 作为url 时,BarControlleractionView 必须有myid(不是id)作为参数。可能你有actionView($id),所以这会导致问题。所以你需要有这样的actionView:

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

【讨论】:

  • 是的,没错。但这是下一步。我刚刚尝试更改控制器操作中的参数名称——没有效果:生成的 URL 看起来像 /index.php/bar/id/123。因为它与 URL 创建无关。它只对 URL 匹配的下一步很重要。所以我想首先创建 URL。
猜你喜欢
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多