AntoCard
AntoCard

Reputation: 23

Yii customizing the display of url parameters

I'm trying to change the way Yii is showing the url for my "product" page. Now, it shows this:

   localhost/~antonio/project/?r=site/product&id=HXW1410D260D0TB013&language=en

Or with urlFormat=path

   localhost/~antonio/project/en/product/id/HXW1410D260D0TB013

I need the url's to look like this:

   localhost/~antonio/project/en/product/HXW1410D260D0TB013

I looked into the Yii docs, but I can't find a way to do this.

Thanks.

Upvotes: 1

Views: 2716

Answers (2)

Afnan Bashir
Afnan Bashir

Reputation: 7429

For this, go to main.php file in config folder and uncomment the following to enable URLs in path format:

'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
),

Upvotes: 0

Avanche
Avanche

Reputation: 1820

Add the following rule to your main.php rules array:

'product/<id:[A-Z0-9]+>'=>'site/product',

so you should have something like

'urlManager' => array(
        'urlFormat' => 'path',
        'showScriptName' => false,
        'rules' => array(
                'product/<id:[A-Z0-9]+>'=>'site/product',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
         ),
  ),

essentially the rule format is as follows:

'product/<id:[A-Z0-9]+>'=>'site/product',

Terms in <> mean you are passing a variable, so

<id:[A-Z0-9]+>

means you are passing $_GET['id'] if the regex matches (if it has only capital letters and numbers).

So the rule above means if the url matches product/something, then send it to site/product and pass the "something" as a $_GET parameter called id.

Hope that clarifies.

Upvotes: 2

Related Questions