urlManager规则上的可选参数 [英] Optional parameter on urlManager rules

查看:82
本文介绍了urlManager规则上的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Yii::app()->request->getParam(),所以我可以拥有一个像/listings/amenities/1这样的友好网址.

I used the Yii::app()->request->getParam() so I can have a friendly url like /listings/amenities/1.

我在控制器上执行了3个操作,这些操作获得了参数$property_id = Yii::app()->request->getParam('property_id').

I got 3 actions on my controller that get the parameter $property_id = Yii::app()->request->getParam('property_id').

两个动作amenitiesmeals都可以正常工作,但是在最后一个动作photos中,变量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.

我尝试删除了照片规则中的第二个参数,并且一切正常.在不删除第二个参数gallery_id的情况下应该如何解决?

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?

以下是urlmanager规则:

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

我认为解决方案涉及如何正确设置可选参数的规则,以处理诸如listings/photos/1listings/photos/1/2之类的请求.添加OR符号并不能解决问题.

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'

推荐答案

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

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

在操作中,将galleryId设置为null:

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

这篇关于urlManager规则上的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆