root/trunk/framework/Web/TUrlManager.php
| Revision 2482, 4.4 kB (checked in by knut, 4 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | <?php |
| 2 | /** |
| 3 | * TUrlManager class file |
| 4 | * |
| 5 | * @author Qiang Xue <qiang.xue@gmail.com> |
| 6 | * @link http://www.pradosoft.com/ |
| 7 | * @copyright Copyright © 2005-2008 PradoSoft |
| 8 | * @license http://www.pradosoft.com/license/ |
| 9 | * @version $Id $ |
| 10 | * @package System.Web |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * TUrlManager class |
| 15 | * |
| 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. |
| 19 | * |
| 20 | * Derived classes may override {@link constructUrl} and {@link parseUrl} |
| 21 | * to provide customized URL schemes. |
| 22 | * |
| 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. |
| 27 | * |
| 28 | * @author Qiang Xue <qiang.xue@gmail.com> |
| 29 | * @version $Id $ |
| 30 | * @package System.Web |
| 31 | * @since 3.0.6 |
| 32 | */ |
| 33 | class TUrlManager extends TModule |
| 34 | { |
| 35 | /** |
| 36 | * Constructs a URL that can be recognized by PRADO. |
| 37 | * |
| 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. |
| 41 | * |
| 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 | * @param string service ID |
| 48 | * @param string service parameter |
| 49 | * @param array GET parameters, null if not provided |
| 50 | * @param boolean whether to encode the ampersand in URL |
| 51 | * @param boolean whether to encode the GET parameters (their names and values) |
| 52 | * @return string URL |
| 53 | * @see parseUrl |
| 54 | */ |
| 55 | public function constructUrl($serviceID,$serviceParam,$getItems,$encodeAmpersand,$encodeGetItems) |
| 56 | { |
| 57 | $url=$serviceID.'='.urlencode($serviceParam); |
| 58 | $amp=$encodeAmpersand?'&':'&'; |
| 59 | $request=$this->getRequest(); |
| 60 | if(is_array($getItems) || $getItems instanceof Traversable) |
| 61 | { |
| 62 | if($encodeGetItems) |
| 63 | { |
| 64 | foreach($getItems as $name=>$value) |
| 65 | { |
| 66 | if(is_array($value)) |
| 67 | { |
| 68 | $name=urlencode($name.'[]'); |
| 69 | foreach($value as $v) |
| 70 | $url.=$amp.$name.'='.urlencode($v); |
| 71 | } |
| 72 | else |
| 73 | $url.=$amp.urlencode($name).'='.urlencode($value); |
| 74 | } |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | foreach($getItems as $name=>$value) |
| 79 | { |
| 80 | if(is_array($value)) |
| 81 | { |
| 82 | foreach($value as $v) |
| 83 | $url.=$amp.$name.'[]='.$v; |
| 84 | } |
| 85 | else |
| 86 | $url.=$amp.$name.'='.$value; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | if($request->getUrlFormat()===THttpRequestUrlFormat::Path) |
| 91 | return $request->getApplicationUrl().'/'.strtr($url,array($amp=>'/','?'=>'/','='=>$request->getUrlParamSeparator())); |
| 92 | else |
| 93 | return $request->getApplicationUrl().'?'.$url; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Parses the request URL and returns an array of input parameters. |
| 98 | * This method is automatically invoked by {@link THttpRequest} when |
| 99 | * handling a user request. |
| 100 | * |
| 101 | * In general, this method should parse the path info part of the requesting URL |
| 102 | * and generate an array of name-value pairs according to some scheme. |
| 103 | * The current implementation deals with both 'Get' and 'Path' URL formats. |
| 104 | * |
| 105 | * You may override this method to support customized URL format. |
| 106 | * @return array list of input parameters, indexed by parameter names |
| 107 | * @see constructUrl |
| 108 | */ |
| 109 | public function parseUrl() |
| 110 | { |
| 111 | $request=$this->getRequest(); |
| 112 | $pathInfo=trim($request->getPathInfo(),'/'); |
| 113 | if($request->getUrlFormat()===THttpRequestUrlFormat::Path && $pathInfo!=='') |
| 114 | { |
| 115 | $separator=$request->getUrlParamSeparator(); |
| 116 | $paths=explode('/',$pathInfo); |
| 117 | $getVariables=array(); |
| 118 | foreach($paths as $path) |
| 119 | { |
| 120 | if(($path=trim($path))!=='') |
| 121 | { |
| 122 | if(($pos=strpos($path,$separator))!==false) |
| 123 | { |
| 124 | $name=substr($path,0,$pos); |
| 125 | $value=substr($path,$pos+1); |
| 126 | if(($pos=strpos($name,'[]'))!==false) |
| 127 | $getVariables[substr($name,0,$pos)][]=$value; |
| 128 | else |
| 129 | $getVariables[$name]=$value; |
| 130 | } |
| 131 | else |
| 132 | $getVariables[$path]=''; |
| 133 | } |
| 134 | } |
| 135 | return $getVariables; |
| 136 | } |
| 137 | else |
| 138 | return array(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ?> |
Note: See TracBrowser
for help on using the browser.
