How To Trigger Urlmanager Conditionally

Now I create a Yii application and configure the urlManager as




'urlManager' => array(

            'urlFormat' => 'path',

            'rules' => array(

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

            ),

            'showScriptName' => false,

        ),



Now I want to access url /index.php?r=site/contact, but I get the default page.

Questions:

  1. Is there any existing configuration method to use the route from _GET[‘r’] or _POST[‘r’] first and then use the urlManager to parseUrl?

  2. I only want to rewrite some controllers or modules, not all urls are rewrited. How can I do?

I create a new url manager to solve the problem,




class UrlManager extends CUrlManager {

    public $excludeRoutePattern = array();

    public function createUrl($route, $params = array(), $ampersand = '&') {

        $matched = false;

        foreach ($this->excludeRoutePattern  as $excludePattern) {

            if (preg_match($excludePattern, $route))

                $matched = true;

        }


        if ($matched) {

            unset($params[$this->routeVar]);

            foreach ($params as $i => $param)

                if ($param === null)

                    $params[$i] = '';


            if (isset($params['#'])) {

                $anchor = '#' . $params['#'];

                unset($params['#']);

            } else

                $anchor = '';

            $route = trim($route, '/');

            return $this->getOriginalUrl($route, $params, $ampersand) . $anchor;

        } else {

            return parent::createUrl($route, $params, $ampersand);

        }

    }


    protected function getOriginalUrl($route, $params, $ampersand) {

        // parent createDefaultUrl + urlFormatter (GET)

        $url = $this->getBaseUrl();

        if (!$this->showScriptName)

            $url.='/';

        if ($route !== '') {

            $url.='?' . $this->routeVar . '=' . $route;

            if (($query = $this->createPathInfo($params, '=', $ampersand)) !== '')

                $url.=$ampersand . $query;

        }

        elseif (($query = $this->createPathInfo($params, '=', $ampersand)) !== '')

            $url.='?' . $query;

        return $url;

    }


    public function parseUrl($request) {

        if (!isset($_GET['r'])) {

            return parent::parseUrl($request);

        } else {

            if (isset($_GET[$this->routeVar]))

                return $_GET[$this->routeVar];

            elseif (isset($_POST[$this->routeVar]))

                return $_POST[$this->routeVar];

            else

                return '';

        }

    }

}







'urlManager' => array(

            'class' => 'application.components.UrlManager',

            'excludeRoutePattern' => array(

                '/^site\//',

            ),

            'urlFormat' => 'path',

            'rules' => array(

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

            ),

            'showScriptName' => false,

        ),