Optional parameter in urlManager rules

I have used Yii::app()->request->getParam()

, so I can have a friendly url like /listings/amenities/1

.

I have 3 actions on my controller that receive a parameter $property_id = Yii::app()->request->getParam('property_id')

.

The two actions amenities

both meals

work fine, but in the last action photos

the var value property_id

got null.

I tried removing the second parameter in the photos rule and everything works. How can I solve this problem without removing the second parameter gallery_id

?

Following are 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 correctly defining the rules for an optional parameter to handle a request like listings/photos/1

and listings/photos/1/2

. Adding the OR character doesn't solve it.

'listings/photos/<property_id>/<gallery_id>'=>'listings/photos'

      

+3


source to share


1 answer


Have you tried using two rules? First, set the longer rule:

'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) {

      

+8


source







All Articles