How to modify URL manager to accept only numbers and not alphabets in yii

436 Views Asked by At

I am working on a project in yii and I am stuck with some kind a issue while when managing Url Manager. Here is the Yii's default given Url manager code

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

Now the problem is with the URLs that take id as an argument. Suppose if my Url is

localhost/project/admin/showMe/view/id/24

then it will take me to the desired place.

But the problem with the above URL manager is that if i will use the URL like localhost/project/admin/showMe/view/id/24someAlphabets then also it takes me to the desired position because the URL manager finds the numbers.

Question:-
So how can i change the URL manager so that it could take me to the desired place only if i pass the numbers and not the alphabets?

1

There are 1 best solutions below

0
On

\d+ will match any number in the string, and will ignore any other letters. If you only want to match numbers and nothing else, you have to use start of string (^) and end of string ($) anchors:

'<controller:\w+>/<id:^\d+$>'=>'<controller>/view',