PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] Yii customizing the display of url parameters

 January 30, 2022     php, url, yii     No comments   

Issue

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.


Solution

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.



Answered By - Avanche
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing