1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TUrlManager class file |
||
5 | * |
||
6 | * @author Qiang Xue <[email protected]> |
||
7 | * @link https://github.com/pradosoft/prado |
||
8 | * @license https://github.com/pradosoft/prado/blob/master/LICENSE |
||
9 | */ |
||
10 | |||
11 | namespace Prado\Web; |
||
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 {@see constructUrl} and {@see parseUrl} |
||
21 | * to provide customized URL schemes. |
||
22 | * |
||
23 | * By default, {@see \Prado\Web\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 {@see \Prado\Web\THttpRequest::setUrlManager THttpRequest.UrlManager} |
||
26 | * with the ID of your URL manager module. |
||
27 | * |
||
28 | * @author Qiang Xue <[email protected]> |
||
29 | * @since 3.0.6 |
||
30 | */ |
||
31 | class TUrlManager extends \Prado\TModule |
||
32 | { |
||
33 | /** |
||
34 | * Constructs a URL that can be recognized by PRADO. |
||
35 | * |
||
36 | * This method provides the actual implementation used by {@see \Prado\Web\THttpRequest::constructUrl}. |
||
37 | * Override this method if you want to provide your own way of URL formatting. |
||
38 | * If you do so, you may also need to override {@see parseUrl} so that the URL can be properly parsed. |
||
39 | * |
||
40 | * The URL is constructed as the following format: |
||
41 | * ``` |
||
42 | * /entryscript.php?serviceID=serviceParameter&get1=value1&... |
||
43 | * ``` |
||
44 | * If {@see \Prado\Web\THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'Path', |
||
45 | * the following format is used instead: |
||
46 | * ``` |
||
47 | * /entryscript.php/serviceID/serviceParameter/get1,value1/get2,value2... |
||
48 | * ``` |
||
49 | * If {@see \Prado\Web\THttpRequest::setUrlFormat THttpRequest.UrlFormat} is 'HiddenPath', |
||
50 | * then entryscript.php will be hidden and the following format is used instead: |
||
51 | * ``` |
||
52 | * /serviceID/serviceParameter/get1,value1/get2,value2... |
||
53 | * ``` |
||
54 | * In order to use the 'HiddenPath' format you need proper url rewrite configuration; |
||
55 | * here's an example for Apache's .htaccess: |
||
56 | * ``` |
||
57 | * Options +FollowSymLinks |
||
58 | * RewriteEngine On |
||
59 | * RewriteCond %{REQUEST_FILENAME} !-d |
||
60 | * RewriteCond %{REQUEST_FILENAME} !-f |
||
61 | * RewriteRule ^(.*)$ index.php/$1 [L] |
||
62 | * ``` |
||
63 | * @param string $serviceID service ID |
||
64 | * @param string $serviceParam service parameter |
||
65 | * @param array $getItems GET parameters, null if not provided |
||
66 | 1 | * @param bool $encodeAmpersand whether to encode the ampersand in URL |
|
67 | * @param bool $encodeGetItems whether to encode the GET parameters (their names and values) |
||
68 | 1 | * @return string URL |
|
69 | 1 | * @see parseUrl |
|
70 | 1 | */ |
|
71 | 1 | public function constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems) |
|
72 | 1 | { |
|
73 | 1 | $url = $serviceID . '=' . urlencode($serviceParam); |
|
74 | 1 | $amp = $encodeAmpersand ? '&' : '&'; |
|
75 | $request = $this->getRequest(); |
||
76 | if (is_array($getItems) || $getItems instanceof \Traversable) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
77 | if ($encodeGetItems) { |
||
78 | foreach ($getItems as $name => $value) { |
||
79 | if (is_array($value)) { |
||
80 | 1 | $name = urlencode($name . '[]'); |
|
81 | foreach ($value as $v) { |
||
82 | $url .= $amp . $name . '=' . urlencode($v); |
||
83 | } |
||
84 | } else { |
||
85 | $url .= $amp . urlencode($name) . '=' . urlencode($value); |
||
86 | } |
||
87 | } |
||
88 | } else { |
||
89 | foreach ($getItems as $name => $value) { |
||
90 | if (is_array($value)) { |
||
91 | foreach ($value as $v) { |
||
92 | $url .= $amp . $name . '[]=' . $v; |
||
93 | } |
||
94 | } else { |
||
95 | $url .= $amp . $name . '=' . $value; |
||
96 | 1 | } |
|
97 | 1 | } |
|
98 | } |
||
99 | 1 | } |
|
100 | |||
101 | switch ($request->getUrlFormat()) { |
||
102 | 1 | case THttpRequestUrlFormat::Path: |
|
103 | return $request->getApplicationUrl() . '/' . strtr($url, [$amp => '/', '?' => '/', '=' => $request->getUrlParamSeparator()]); |
||
104 | case THttpRequestUrlFormat::HiddenPath: |
||
105 | return rtrim(dirname($request->getApplicationUrl()), '/') . '/' . strtr($url, [$amp => '/', '?' => '/', '=' => $request->getUrlParamSeparator()]); |
||
106 | default: |
||
107 | return $request->getApplicationUrl() . '?' . $url; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Parses the request URL and returns an array of input parameters. |
||
113 | * This method is automatically invoked by {@see \Prado\Web\THttpRequest} when |
||
114 | * handling a user request. |
||
115 | * |
||
116 | * In general, this method should parse the path info part of the requesting URL |
||
117 | * and generate an array of name-value pairs according to some scheme. |
||
118 | * The current implementation deals with both 'Get' and 'Path' URL formats. |
||
119 | 10 | * |
|
120 | * You may override this method to support customized URL format. |
||
121 | 10 | * @return array list of input parameters, indexed by parameter names |
|
122 | 10 | * @see constructUrl |
|
123 | 10 | */ |
|
124 | 10 | public function parseUrl() |
|
125 | 10 | { |
|
126 | $request = $this->getRequest(); |
||
127 | $pathInfo = urldecode(trim($request->getPathInfo(), '/')); |
||
128 | if (($request->getUrlFormat() === THttpRequestUrlFormat::Path || |
||
129 | $request->getUrlFormat() === THttpRequestUrlFormat::HiddenPath) && |
||
0 ignored issues
–
show
|
|||
130 | $pathInfo !== '') { |
||
131 | $separator = $request->getUrlParamSeparator(); |
||
132 | $paths = explode('/', $pathInfo); |
||
133 | $getVariables = []; |
||
134 | foreach ($paths as $path) { |
||
135 | if (($path = trim($path)) !== '') { |
||
136 | if (($pos = strpos($path, $separator)) !== false) { |
||
137 | $name = substr($path, 0, $pos); |
||
138 | $value = substr($path, $pos + 1); |
||
139 | if (($pos = strpos($name, '[]')) !== false) { |
||
140 | $getVariables[substr($name, 0, $pos)][] = $value; |
||
141 | } else { |
||
142 | $getVariables[$name] = $value; |
||
143 | } |
||
144 | } else { |
||
145 | $getVariables[$path] = ''; |
||
146 | 10 | } |
|
147 | } |
||
148 | } |
||
149 | return $getVariables; |
||
150 | } else { |
||
151 | return []; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 |