Optional parameter on urlManager rules
Asked Answered
I

1

10

I used the Yii::app()->request->getParam() so I can have a friendly url like /listings/amenities/1.

I got 3 actions on my controller that get the parameter $property_id = Yii::app()->request->getParam('property_id').

The two actions amenities and meals are working fine but in the last action photos, the var property_id got a null value.

I tried removing the second param on the photos rule and everything works. How should I solve this without removing the second param gallery_id?

Below is the urlmanager rules:

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName' => false,
            'rules'=>array(
                'listings/amenities/<property_id>'=>'listings/amenities',
                'listings/meals/<property_id>'=>'listings/meals',
                'listings/photos/<property_id>/<gallery_id>'=>'listings/photos',
             ),
         ),

[EDIT] I think the solution involves how to properly set the rules for optional parameter to handle request like listings/photos/1 and listings/photos/1/2. Adding the OR symbol does not solve it.

'listings/photos/<property_id>/<gallery_id>'=>'listings/photos'
Integer answered 9/9, 2014 at 12:57 Comment(1)
finally figure it out, just need to put separate rules 'listings/photos/<property_id>'=>'listings/photos' and 'listings/photos/<property_id>/<gallery_id>'=>'listings/photos'Integer
N
18

Have you tried using two rules? Place the longer (more restrictive) rule first:

'listings/photos/<property_id:\d+>/<gallery_id:\d+>' => 'listings/photos',
'listings/photos/<property_id:\d+>' => 'listings/photos',

In your action, set galleryId to null:

public function actionPhotos($propertyId, $galleryId = null) {
Nutlet answered 9/9, 2014 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.