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