Bujji
Bujji

Reputation: 1727

URL Manager - action parameter optional and link creation

I have below URL mapping in my main.php file . You can see I am expecting parameter id as an optional

'urlManager'=>array(
        'urlFormat'=>'path',                    
                    'showScriptName'=>false,
                   'urlSuffix' => '/',
                    'useStrictParsing' => true,
        'rules'=>array(
                           '' => 'site/index',                            
                            '<action:(signIn|signUp|logout)>' => 'site/<action>',
                            '<controller:\w+>(/<id:\d+>)?'=>'<controller>/index',
        '<controller:\w+>/<action:\w+>(/<id:\d+>)?'=>'<controller>/<action>',
        ),
    ),

with the above rules If I create a link using below code

<?php echo CHtml::link('×', array('index', 'id' => $this->group_id), array('class' => 'linkclose')); ?>  

this creates link some thing like below , which is wrong

http://localhost/blog(/872280)?/

It should generate some thing like below

http://localhost/blog/872280/

If I don't pass the parameter in the link I mean

<?php echo CHtml::link('×', array('index'), array('class' => 'linkclose')); ?>  

this generates

http://localhost/blog/index

which is fine .

But with the parameter passing it is messing up the link .. Could some one help me on this ? Thanks

Upvotes: 0

Views: 88

Answers (1)

DaSourcerer
DaSourcerer

Reputation: 6606

URL rules in their entirety are not regular expressions. Thus, optional groups like (...)? aren't honored. You can work around this by specifying alternative rules:

'rules'=>array(
    ...
    '<controller:\w+>/<id:\d+>'=>'<controller>/index',
    '<controller:\w+>'=>'<controller>/index',
    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),

Upvotes: 1

Related Questions