Passing multiple parameters in a hyperlink in yii2 with clean urls, Html::a() doesnt generate clean url

ぐ巨炮叔叔 提交于 2019-12-22 03:48:16

问题


I am trying to generate a hyper link by the method mentioned in http://www.yiiframework.com/doc-2.0/guide-helper-html.html#hyperlinks like this

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

I want to get url like story/create/id/39/usr/11

But it is generating as

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

I have enabled the clean url functionality of yii2 like

  '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.

How this can be achieved?


回答1:


With generate url use like that (see more 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'])

In urlManager input new rule:

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

        ),

Output url will be like that:

story/create/39/11

And in controller:

public function actionCreate($id, $usr)

And Yii2 provide this parameter.




回答2:


create Url Dynamically

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

In urlManager config rules :

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

Output url will be like that:

story/create/39/11



回答3:


Another useful method :

Write in urlManager rules in your

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

Can be accessed in url controller/action/100/20



来源:https://stackoverflow.com/questions/29250207/passing-multiple-parameters-in-a-hyperlink-in-yii2-with-clean-urls-htmla-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!