JaquelineBrandao / yii

Automatically exported from code.google.com/p/yii
0 stars 0 forks source link

Improved CUrlManager->parsePathInfo #923

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The current function doesn't work fine with multiarrays.
For example "/Post[Category]/23" in url will be Post=array('Category'=>23)
BUT "/Post[Category][Subcategory]/23" will NOT be
Post=array(Category=>array(Subcategory=>23))

this is my version of this function

public static function parsePathInfo($pathInfo)
    {
        function recursivePathInfoArrayParser($key,$val)
        {
            while (($pos=strpos($key,'['))!==false &&
($pos2=strpos($key,']',$pos+1))!==false)
            {
                if($pos2!=$pos+1)
                {
                    $ckey=substr($key,$pos+1,$pos2-$pos-1);
                }
                if ($ckey)
                {
                    return
$result=array($ckey=>recursivePathInfoArrayParser(substr($key,$pos2+1,strlen($ke
y)-$pos2),$val));
                }
                else
                {
                    return
$result=array($ckey=>recursivePathInfoArrayParser(substr($key,$pos2+1,strlen($ke
y)-$pos2),$val));
                }
            }
            return $val;
        };

        if($pathInfo==='')
            return;
        $segs=explode('/',$pathInfo.'/');
        $n=count($segs);
        for($i=0;$i<$n-1;$i+=2)
        {
            $key=$segs[$i];
            if($key==='') continue;
            $value=$segs[$i+1];
            if(($pos=strpos($key,'['))!==false &&
($pos2=strpos($key,']',$pos+1))!==false)
            {
                $name=substr($key,0,$pos);

$value=recursivePathInfoArrayParser(substr($key,substr($key,$pos2+1,strlen($key)
-$pos2)),$value);
                if (!is_array($_REQUEST[$name])) {$_REQUEST[$name]=array();};
                $value=array_merge_recursive($_REQUEST[$name],$value);
                $_REQUEST[$name]=$_GET[$name]=$value;
            }
            else
                $_REQUEST[$key]=$_GET[$key]=$value;
        }
    }

it can and have troubles with code Conventions but it works good for mee,
wish fixed version of main developers will be included to the framework

Original issue reported on code.google.com by Unloved....@gmail.com on 12 Feb 2010 at 1:25

GoogleCodeExporter commented 9 years ago
sorry for my bad english and lame pasting text of code 

Original comment by Unloved....@gmail.com on 12 Feb 2010 at 1:27

GoogleCodeExporter commented 9 years ago
found some bugs, here is new version (recursion is slowing but it's my only 
version
how to solve the problem)

        public static function recursivePathInfoArrayParser($key,$val)
        {
            while (($pos=strpos($key,'['))!==false &&
($pos2=strpos($key,']',$pos+1))!==false)
            {
                if($pos2!=$pos+1)
                {
                    $ckey=substr($key,$pos+1,$pos2-$pos-1);
                }
                if ($ckey)
                {
                    return
$result=array($ckey=>self::recursivePathInfoArrayParser(substr($key,$pos2+1,strl
en($key)-$pos2),$val));
                }
                else
                {
                    return
$result=array($ckey=>self::recursivePathInfoArrayParser(substr($key,$pos2+1,strl
en($key)-$pos2),$val));
                }
            }
            return $val;
        }

    /**
     * Parses a path info into URL segments and saves them to $_GET and $_REQUEST.
     * @param string path info
     * @since 1.0.3
     */
    public static function parsePathInfo($pathInfo)
    {

            if($pathInfo==='') return;
            $segs=explode('/',$pathInfo.'/');
            $n=count($segs);
            for($i=0;$i<$n-1;$i+=2)
            {
                    $key=$segs[$i];
                    if($key==='') continue;
                    $value=$segs[$i+1];
                    if(($pos=strpos($key,'['))!==false &&
($pos2=strpos($key,']',$pos+1))!==false)
                    {
                            $name=substr($key,0,$pos);

$value=self::recursivePathInfoArrayParser(substr($key,substr($key,$pos2+1,strlen
($key)-$pos2)),$value);
                            if (!is_array($_REQUEST[$name])) {$_REQUEST[$name]=array();};
                            $value=array_merge_recursive($_REQUEST[$name],$value);
                            $_REQUEST[$name]=$_GET[$name]=$value;
                    }
                    else
                    $_REQUEST[$key]=$_GET[$key]=$value;
            }
    }

Original comment by Unloved....@gmail.com on 12 Feb 2010 at 2:57

GoogleCodeExporter commented 9 years ago

Original comment by qiang.xue on 27 Feb 2010 at 3:44

GoogleCodeExporter commented 9 years ago
Move to 1.1.3 release.

Original comment by qiang.xue on 2 May 2010 at 2:19

GoogleCodeExporter commented 9 years ago

Original comment by qiang.xue on 6 Jul 2010 at 4:57

GoogleCodeExporter commented 9 years ago
Move to 1.1.4.

Original comment by qiang.xue on 2 Sep 2010 at 4:09

GoogleCodeExporter commented 9 years ago
1.1.5

Original comment by qiang.xue on 2 Sep 2010 at 4:09

GoogleCodeExporter commented 9 years ago
Out of time. Set for 1.1.6 milestone.

Original comment by qiang.xue on 12 Nov 2010 at 1:54

GoogleCodeExporter commented 9 years ago
Another option would be to use something like:

preg_match_all('/\[(.*?)\]/',$name,$m);

i'm just not sure about how bad this would affect performace, though. Maybe 
first test for a '[' in $key and only use the regex if it contains one.

Original comment by haertl.mike@gmail.com on 15 Dec 2010 at 9:02

GoogleCodeExporter commented 9 years ago
The other question is, if we need to do full recursion, or if 2 levels would be 
sufficient. The most common scenario i have is a filter form (method=get) with 
a checkboxlist to select multiple filter values for an attribute. If there's a 
grid view on that page, the pager links will contain path-encoded filter 
attributes.

More details here:
http://www.yiiframework.com/forum/index.php?/topic/14178-allow-array-attributes-
with-urlformat-path/

Original comment by haertl.mike@gmail.com on 15 Dec 2010 at 9:08

GoogleCodeExporter commented 9 years ago
Set for next milestone.

Original comment by qiang.xue on 15 Jan 2011 at 7:25

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r3100.

Original comment by qiang.xue on 19 Mar 2011 at 6:10