Optional parameter on urlManager rules

十年热恋 提交于 2019-12-12 10:35:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!