Reputation: 23
I need to make url with some of parametres been as query string, like:
site.com/controller/action/param1/param2?param3=value¶m4=value
Can someone help me with this problem?
Upvotes: 0
Views: 139
Reputation: 23
I found the solution. I created component extended from 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;
}
}
I know - it is to match code but it is work and easy to customize.
Upvotes: 0
Reputation: 2267
You should set this rule in config:
'rules'=>array(
array('controller/action', 'pattern'=>'controller/action/<param1:\w+>/<param2:\w+>'),
)
and to create same url:
Yii::app()->createAbsoluteUrl(
'controller/action',
array('param1'=>'param1value', 'param2'=>'param2value', 'param3'=>'param3value', 'param4'=>'param4value'
);
and you get this url:
http://example.com/controller/action/param1value/param2value?param3=param3value¶m4=param4value
and get parameters will be
$_GET['param1'] //'param1value'
$_GET['param2'] //'param2value'
$_GET['param3'] //'param3value'
$_GET['param4'] //'param4value'
Upvotes: 1