11

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?

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
user7282
  • 5,106
  • 9
  • 41
  • 72

3 Answers3

28

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.

vitalik_74
  • 4,573
  • 2
  • 19
  • 27
  • it is still generating like story/create?1%5Bid%5D=39&1%5Busr%5D=1 – user7282 Mar 25 '15 at 08:10
  • In `Html` in set `['story/create', 'id' =>39,'usr'=>'11'], `? – vitalik_74 Mar 25 '15 at 08:25
  • It is now creating like reg/create/39?usr=1 , I need ? to be removed – user7282 Mar 25 '15 at 08:33
  • I tried and it is work fine. look - https://yadi.sk/i/neiWnrkufW43J and https://yadi.sk/i/pSvwF5GSfW49Z and my `.htaccess` https://yadi.sk/i/4jsoxNwffW4D9 – vitalik_74 Mar 25 '15 at 08:47
  • I dont know it is like create/39?usr=1 , may be some thing related to generation of hyperlink by Html::a() – user7282 Mar 25 '15 at 09:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73740/discussion-between-vitalik-74-and-user7282). – vitalik_74 Mar 25 '15 at 09:31
  • I got it working , this should be in the first line of rules, it it is at the end it generates '?'You may verify it or find a better solution – user7282 Mar 25 '15 at 09:42
  • After you send it you can get the data with: print_r(Yii::$app->request->get()['id']; and print_r(Yii::$app->request->get()['usr'] ); – Roby Sottini Nov 30 '18 at 14:31
1

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
Rahman
  • 282
  • 1
  • 10
0

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

Shubham
  • 37
  • 4