Yii Framework Forum: URL rules with Array Params - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

URL rules with Array Params Rate Topic: -----

#1 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 01 May 2010 - 04:06 AM

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
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#2 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 01 May 2010 - 04:37 AM

I think that's because [] are reserved chars in the regex. Try escaping them with \[ \]
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#3 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 01 May 2010 - 05:45 AM

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
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#4 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 02 May 2010 - 09:01 AM

Wontfix :(
http://code.google.c.../detail?id=1181
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#5 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 02 May 2010 - 10:37 AM

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.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users