3 * TUrlManager class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
16 * TUrlManager is the base class for managing URLs that can be
17 * recognized by PRADO applications. It provides the default implementation
18 * for parsing and constructing URLs.
20 * Derived classes may override {@link constructUrl} and {@link parseUrl}
21 * to provide customized URL schemes.
23 * By default, {@link THttpRequest} uses TUrlManager as its URL manager.
24 * If you want to use your customized URL manager, load your manager class
25 * as an application module and set {@link THttpRequest::setUrlManager THttpRequest.UrlManager}
26 * with the ID of your URL manager module.
28 * @author Qiang Xue <qiang.xue@gmail.com>
33 class TUrlManager extends TModule
36 * Constructs a URL that can be recognized by PRADO.
38 * This method provides the actual implementation used by {@link THttpRequest::constructUrl}.
39 * Override this method if you want to provide your own way of URL formatting.
40 * If you do so, you may also need to override {@link parseUrl} so that the URL can be properly parsed.
42 * The URL is constructed as the following format:
43 * /entryscript.php?serviceID=serviceParameter&get1=value1&...
44 * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'Path',
45 * the following format is used instead:
46 * /entryscript.php/serviceID/serviceParameter/get1,value1/get2,value2...
47 * If {@link THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'HiddenPath',
48 * then entryscript.php will be hidden and the following format is used instead:
49 * /serviceID/serviceParameter/get1,value1/get2,value2...
50 * In order to use the 'HiddenPath' format you need proper url rewrite configuration;
51 * here's an example for Apache's .htaccess:
53 * Options +FollowSymLinks
55 * RewriteCond %{REQUEST_FILENAME} !-d
56 * RewriteCond %{REQUEST_FILENAME} !-f
57 * RewriteRule ^(.*)$ index.php/$1 [L]
59 * @param string service ID
60 * @param string service parameter
61 * @param array GET parameters, null if not provided
62 * @param boolean whether to encode the ampersand in URL
63 * @param boolean whether to encode the GET parameters (their names and values)
67 public function constructUrl($serviceID,$serviceParam,$getItems,$encodeAmpersand,$encodeGetItems)
69 $url=$serviceID.'='.urlencode($serviceParam);
70 $amp=$encodeAmpersand?'&':'&';
71 $request=$this->getRequest();
72 if(is_array($getItems) || $getItems instanceof Traversable)
76 foreach($getItems as $name=>$value)
80 $name=urlencode($name.'[]');
82 $url.=$amp.$name.'='.urlencode($v);
85 $url.=$amp.urlencode($name).'='.urlencode($value);
90 foreach($getItems as $name=>$value)
95 $url.=$amp.$name.'[]='.$v;
98 $url.=$amp.$name.'='.$value;
103 switch($request->getUrlFormat())
105 case THttpRequestUrlFormat::Path:
106 return $request->getApplicationUrl().'/'.strtr($url,array($amp=>'/','?'=>'/','='=>$request->getUrlParamSeparator()));
107 case THttpRequestUrlFormat::HiddenPath:
108 return rtrim(dirname($request->getApplicationUrl()), '/').'/'.strtr($url,array($amp=>'/','?'=>'/','='=>$request->getUrlParamSeparator()));
110 return $request->getApplicationUrl().'?'.$url;
115 * Parses the request URL and returns an array of input parameters.
116 * This method is automatically invoked by {@link THttpRequest} when
117 * handling a user request.
119 * In general, this method should parse the path info part of the requesting URL
120 * and generate an array of name-value pairs according to some scheme.
121 * The current implementation deals with both 'Get' and 'Path' URL formats.
123 * You may override this method to support customized URL format.
124 * @return array list of input parameters, indexed by parameter names
127 public function parseUrl()
129 $request=$this->getRequest();
130 $pathInfo=trim($request->getPathInfo(),'/');
131 if(($request->getUrlFormat()===THttpRequestUrlFormat::Path ||
132 $request->getUrlFormat()===THttpRequestUrlFormat::HiddenPath) &&
135 $separator=$request->getUrlParamSeparator();
136 $paths=explode('/',$pathInfo);
137 $getVariables=array();
138 foreach($paths as $path)
140 if(($path=trim($path))!=='')
142 if(($pos=strpos($path,$separator))!==false)
144 $name=substr($path,0,$pos);
145 $value=substr($path,$pos+1);
146 if(($pos=strpos($name,'[]'))!==false)
147 $getVariables[substr($name,0,$pos)][]=$value;
149 $getVariables[$name]=$value;
152 $getVariables[$path]='';
155 return $getVariables;