yii2 - 使用干净的 url 在 yii2 中的超链接中传递多个参数,Html::a() 不会生成干净的 url

标签 yii2 clean-urls yii-url-manager

我正在尝试通过中提到的方法生成超链接
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 的干净 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.

如何实现?

最佳答案

与生成 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',

        ),

输出 url 将是这样的:
story/create/39/11

在 Controller 中:
public function actionCreate($id, $usr)

而 Yii2 提供了这个参数。

关于yii2 - 使用干净的 url 在 yii2 中的超链接中传递多个参数,Html::a() 不会生成干净的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29250207/

相关文章:

php - Yii2:登录时设置不正确的用户名或密码消息

css - Yii2:为什么在 Firefox 中多次加载 bootstrap.css?

用于解析干净 URL 并获取其不同组件的 Java 库/类

php - 如何使用/web 以外的 web 根目录部署 yii2

Yii2 清理 URL 和操作参数

yii2 - 如果新值等于上一个值,则跳过 yii2 对更新操作的唯一验证

php - Yii2错误: The view file does not exist

javascript - 使用正则表达式从 JavaScript 中的 URL 中删除 utm_* 参数

php - Mod_Rewrite 规则删除本地主机上的 .php 文件扩展名

yii2 - Yii 2 静态页面