URL rules with Array Params


(Schmunk) #1

Hi guys,

is it possible to rewrite a URL with array parameters like:


/frontend/index?Project[product_id]=2

to


/frontend/product/2

I had no luck with:


'/frontend/product/<Project[product_id]:\d+>'=>'frontend/index',

Can CUrlManager do this?

Best regards,

schmunk


(Vadimg88) #2

I think that’s because [] are reserved chars in the regex. Try escaping them with \[ \]


(Schmunk) #3

Hi Vince,


'frontend/<Project\[client_id\]:\d+>'=>'frontend/index',

thanks, but then this error occurs:




PHP Error


Description


urlencode() expects parameter 1 to be string, array given


Source File


/Users/tobias/Webserver/NetBeans/hk-2010/lib/yii/web/CUrlManager.php(630)


00618: 

00619:         if($manager->matchValue && $this->matchValue===null || $this->matchValue)

00620:         {

00621:             foreach($this->params as $key=>$value)

00622:             {

00623:                 if(!preg_match('/'.$value.'/'.$case,$params[$key]))

00624:                     return false;

00625:             }

00626:         }

00627: 

00628:         foreach($this->params as $key=>$value)

00629:         {

00630:             $tr["<$key>"]=urlencode($params[$key]);

00631:             unset($params[$key]);

00632:         }

00633: 

00634:         $suffix=$this->urlSuffix===null ? $manager->urlSuffix : $this->urlSuffix;

00635: 

00636:         $url=strtr($this->template,$tr);

00637:         if(empty($params))

00638:             return $url!=='' ? $url.$suffix : $url;

00639: 

00640:         if($this->append)

00641:             $url.='/'.$manager->createPathInfo($params,'/','/').$suffix;

00642:         else



I also had this problem with another piece of code.

It’s definitely a problem that you’ll never have a “real” array within URL params, but PHP parses them into arrays.

Best regards,

schmunk


(Schmunk) #4

Wontfix :(

http://code.google.com/p/yii/issues/detail?id=1181


(Schmunk) #5

Ok, to complete this, that’s my solution:


   public function beforeAction($action){

        foreach($_GET AS $key => $value) {

            $modelName = strstr($key,"_",true);

            switch ($modelName) {

                case 'Project';

                    $_GET[$modelName][substr(str_replace($modelName, "", $key),1)] = $value;

                    break;

            }            

        }

        return true;

    }



Looks for a certain prefix and parses it into $_GET.