Search code examples
yiiyii-url-manager

Yii UrlManager partial render of URL


I need to make url with some of parametres been as query string, like:

site.com/controller/action/param1/param2?param3=value&param4=value

Can someone help me with this problem?


Solution

  • 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.