问题
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'
回答1:
Have you tried using two rules? Place the longer 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) {
来源:https://stackoverflow.com/questions/25745249/optional-parameter-on-urlmanager-rules