我需要将带有一些参数的url作为查询字符串,例如:
site.com/controller/action/param1/param2?param3=value¶m4=value
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您应该在config中设置此规则:
'rules'=>array(
array('controller/action', 'pattern'=>'controller/action/<param1:\w+>/<param2:\w+>'),
)
并创建相同的网址:
Yii::app()->createAbsoluteUrl(
'controller/action',
array('param1'=>'param1value', 'param2'=>'param2value', 'param3'=>'param3value', 'param4'=>'param4value'
);
你得到这个网址:
http://example.com/controller/action/param1value/param2value?param3=param3value¶m4=param4value
并获取参数
$_GET['param1'] //'param1value'
$_GET['param2'] //'param2value'
$_GET['param3'] //'param3value'
$_GET['param4'] //'param4value'
答案 1 :(得分:0)
我找到了解决方案。 我创建了从CBaseUrlRule扩展的组件。
class CCustomUrlRule extends CBaseUrlRule {
private $url;
public function init()
{
if ($this->name === null) {
$this->name = __CLASS__;
}
}
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route === 'controller/index') {
$this->url = 'controller/';
if (isset($params['param1'])) {
$this->url .= 'param1/' . $params['param1'] .'/';
}
if (isset($params['param2'])) {
$this->url .= 'param2/' . $params['param2'] .'/';
}
$this->url = substr($this->url, 0, -1);
if (isset($params['param3']) || isset($params['param4'])) {
$this->url .= '?';
if (isset($params['param3']) && isset($params['param4'])) {
$this->url .= 'param3=' . $params['param3'] .'&';
$this->url .= 'param4=' . $params['param4'];
}
elseif (isset($params['param3'])) {
$this->url .= 'param3=' . $params['param3'];
}
else {
$this->url .= 'param4=' . $params['param4'];
}
}
return $this->url;
}
return false;
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
return false;
}
}
我知道 - 这是为了匹配代码,但它很容易定制。