Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameter on urlManager rules

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'
like image 758
Pelang Avatar asked Sep 09 '14 12:09

Pelang


1 Answers

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) {
like image 70
Samuel Liew Avatar answered Oct 03 '22 22:10

Samuel Liew