Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 782
user7282 Avatar asked Mar 25 '15 07:03

user7282


People also ask

What is url rule in Yii?

A URL rule is a class implementing the yii\web\UrlRuleInterface, usually yii\web\UrlRule. Each URL rule consists of a pattern used for matching the path info part of URLs, a route, and a few query parameters. A URL rule can be used to parse a request if its pattern matches the requested URL.

How to do url rewiring in yii2 framework?

Module_Rewrite is enabled on your server (LAMP,WAMP,XAMP..etc) For do URL rewiring in yii2 framework Create one .htaccess file and put in /web folder

How to pass parameters using hyperlink in HTML?

Here Mudassar Ahmed Khan has explained with an example, how to pass (send) Parameters using HyperLink in HTML. The HTML HyperLink will be assigned an OnClick event handler and when clicked, the value of the TextBox will be set in the HREF attribute of the HTML HyperLink using JavaScript.

How to pass multiple parameters in a URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


2 Answers

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.

like image 128
vitalik_74 Avatar answered Oct 11 '22 22:10

vitalik_74


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
like image 25
Rahman Avatar answered Oct 11 '22 22:10

Rahman