4

I am using yii2.I have problem related to url structure. How I can change URL structure in Yii2 my current URL is given below

http://localhost/advanced/posts/view?id=1

My expected URL is

http://localhost/advanced/posts/view/id/1

I have follow the following link to change default URL of Yii2

Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

Community
  • 1
  • 1
Ram Choubey
  • 265
  • 2
  • 11

2 Answers2

3

Web.php

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

If Having alpha numeric parameter, then use.

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

For More Info, click URL Not Accepting Alpha Numeric Paramater

Community
  • 1
  • 1
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
  • Suggest using this instead of your rule; `'//' => '/'`, it's more precise and doesn't allow letters in the id – Joe Miller Dec 19 '15 at 14:57
  • Ok. @JoeMiller . But, why i wrote `` because if parameters will be like 41a. Means alpha numeric. Then, it can be helpful. In ``, alpha numerica parameter are not accepted. – Nana Partykar Dec 19 '15 at 14:59
  • Only because he's using an id as the parameter, id's are normally numeric only so that they can auto-increment in the database, otherwise you'd be better using ``, which will allow any normal word characters (letters and numbers). If you only have `[a-z0-9]` then it won't allow international characters, whereas I think `w+` will. – Joe Miller Dec 19 '15 at 15:04
  • So, what you suggest @JoeMiller. `` or `` ? – Nana Partykar Dec 19 '15 at 15:05
  • For the OPs situation, as he's described it, ``would be better – Joe Miller Dec 19 '15 at 15:08
1
'components' => [
    'urlManager' => [               
        'showScriptName' => false,  // Disable index.php
        'enablePrettyUrl' => true, // Disable r= routes
        'enableStrictParsing' => true,
        'rules' => array(
                'mycategory/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                //Rules with Server Names
                'http://admin.domain.com/login' => 'admin/user/login',
                'http://www.domain.com/login' => 'site/login',
                'http://<country:\w+>.domain.com/profile' => 'user/view',
                '<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
            ),
    ],
],

and follow this link : first link second link

Amitesh Kumar
  • 2,903
  • 22
  • 39