urlManager规则的可选参数

时间:2014-09-09 12:57:11

标签: yii parameters yii-url-manager

我使用了Yii::app()->request->getParam(),因此我可以拥有一个友好的网址,例如/listings/amenities/1

我的控制器上有3个操作,它们获取参数$property_id = Yii::app()->request->getParam('property_id')

两个操作amenitiesmeals工作正常,但在上一个操作photos中,var property_id获得了空值。

我尝试删除照片规则上的第二个参数,一切正常。如何解决这个问题而不删除第二个参数gallery_id

以下是urlmanager规则:

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

[编辑] 我认为该解决方案涉及如何正确设置可选参数的规则来处理listings/photos/1listings/photos/1/2之类的请求。添加OR符号无法解决问题。

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

1 个答案:

答案 0 :(得分:11)

您是否尝试过使用两条规则?首先放置较长的规则:

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

在您的操作中,将galleryId设置为null

public function actionPhotos($propertyId, $galleryId = null) {