11

我正在尝试通过 http://www.yiiframework.com/doc-2.0/guide-helper-html.html#hyperlinks中提到的方法生成一个超链接,就像这样

 Html::a('<b>Register</b>', 
    ['story/create', array('id' =>39,'usr'=>'11')], 
    ['class' => 'profile-link'])

我想得到像 story/create/id/39/usr/11

但它正在生成

story/create?1%5Bid%5D=39&1%5Busr%5D=1

我已经启用了 yii2 的 clean url 功能,比如

  'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
        ], also.

如何做到这一点?

4

3 回答 3

28

像这样使用生成url(查看更多http://www.yiiframework.com/doc-2.0/guide-helper-url.html):

Html::a('<b>Register</b>', 
        ['story/create', 'id' =>39,'usr'=>'11'], 
        ['class' => 'profile-link'])

在 urlManager 中输入新规则:

rules' => array(
  ....
  'story/create/<id:\d+>/<usr:\d+>' => 'story/create',

        ),

输出网址将是这样的:

story/create/39/11

在控制器中:

public function actionCreate($id, $usr)

Yii2 提供了这个参数。

于 2015-03-25T08:03:50.197 回答
1

动态创建 URL

Html::a('<b>Register</b>', 
    ['story/create', 'id' =>39,'usr'=>'11'], 
    ['class' => 'profile-link'])

在 urlManager 配置规则中:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
             '<controller:\w+>/<id:\d+>' => '<controller>/view',            
             '<controller:\w+>/<action:\w+>/<id:\d+>/<usr:\d+>' => '<controller>/<action>', 
        ],
    ],

输出网址将是这样的:

story/create/39/11
于 2016-07-28T04:48:21.340 回答
0

另一个有用的方法:

在您的 urlManager 规则中写入

'rules'=>array('/controller/action/<limit>/<offset>'=>'/controller/action/'),

可以在url controller/action/100/20中访问

于 2017-12-07T11:18:30.143 回答