| 1 |
lars |
1 |
<com:TContent ID="body" >
|
|
|
2 |
|
|
|
3 |
<h1 id="2101">URL Mapping (Friendly URLs)</h1>
|
|
|
4 |
|
|
|
5 |
<com:DocLink ClassPath="System.Web.TUrlMapping" />
|
|
|
6 |
|
|
|
7 |
<p id="230222" class="block-content">
|
|
|
8 |
The TUrlMapping module allows PRADO to construct and recognize friendly URLs
|
|
|
9 |
based on specific patterns.
|
|
|
10 |
</p>
|
|
|
11 |
|
|
|
12 |
<p id="230223" class="block-content">
|
|
|
13 |
TUrlMapping consists of a list of URL patterns which are used to match
|
|
|
14 |
against the currently requested URL. The first matching pattern will then
|
|
|
15 |
be used to decompose the URL into request parameters (accessible via
|
|
|
16 |
<tt>$this->Request['paramname']</tt>). The patterns can also be used to
|
|
|
17 |
construct customized URLs. In this case, the parameters in an applied
|
|
|
18 |
pattern will be replaced with the corresponding GET variable values.
|
|
|
19 |
</p>
|
|
|
20 |
|
|
|
21 |
<p id="230224" class="block-content">
|
|
|
22 |
To use <tt>TUrlMapping</tt>, one must set the <tt>UrlManager</tt> property of the <tt>THttpRequest</tt> module as the <tt>TUrlMapping</tt> module ID. See following for an example,
|
|
|
23 |
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_230098">
|
|
|
24 |
<modules>
|
|
|
25 |
<module id="request" class="THttpRequest" UrlManager="friendly-url" />
|
|
|
26 |
<module id="friendly-url" class="System.Web.TUrlMapping">
|
|
|
27 |
<url ServiceParameter="Posts.ViewPost" pattern="post/{id}/" parameters.id="\d+" />
|
|
|
28 |
<url ServiceParameter="Posts.ListPost" pattern="archive/{time}/" parameters.time="\d{6}" />
|
|
|
29 |
<url ServiceParameter="Posts.ListPost" pattern="category/{cat}/" parameters.cat="\d+" />
|
|
|
30 |
</module>
|
|
|
31 |
</modules>
|
|
|
32 |
</com:TTextHighlighter>
|
|
|
33 |
</p>
|
|
|
34 |
|
|
|
35 |
<p id="230225" class="block-content">
|
|
|
36 |
The above example is part of the application configuration of the <tt>blog</tt> demo in the PRADO release. It enables recognition of the following URL formats:
|
|
|
37 |
</p>
|
|
|
38 |
<ul id="u1" class="block-content">
|
|
|
39 |
<li><tt>/index.php/post/123</tt> is recognized as <tt>/index.php?page=Posts.ViewPost&id=123</tt></li>
|
|
|
40 |
<li><tt>/index.php/archive/200605</tt> is recognized as <tt>/index.php?page=Posts.ListPost&time=200605</tt></li>
|
|
|
41 |
<li><tt>/index.php/category/2</tt> is recognized as <tt>/index.php?page=Posts.ListPost&cat=2</tt></li>
|
|
|
42 |
</ul>
|
|
|
43 |
|
|
|
44 |
<p id="230226" class="block-content">
|
|
|
45 |
The <tt>ServiceParameter</tt> and <tt>ServiceID</tt> (the default ID is 'page')
|
|
|
46 |
set the service parameter and service ID, respectively, of the <a href="?page=Fundamentals.Modules">Request module</a>. The service parameter for the <tt>TPageService</tt> service is the Page class name, e.g., for an URL "index.php?page=Home", "page" is the service ID and the service parameter is "Home". Other services may use the service parameter and ID differently. See <a href="?page=Fundamentals.Services">Services</a> for further details.
|
|
|
47 |
</p>
|
|
|
48 |
|
|
|
49 |
<div class="info"><b class="tip">Info:</b>
|
|
|
50 |
The <tt>TUrlMapping</tt> must be configured before the
|
|
|
51 |
<a href="?page=Fundamentals.Modules">request module</a> resolves the request.
|
|
|
52 |
This means delcaring the <tt>TUrlMapping</tt> outside of the
|
|
|
53 |
<tt><services></tt> element in the <a href="?page=Configurations.AppConfig">application configuration</a>.
|
|
|
54 |
Specifying the mappings in the per directory <tt>config.xml</tt> is not supported.
|
|
|
55 |
</div>
|
|
|
56 |
|
|
|
57 |
<h2 id="46023">Specifying URL Patterns</h2>
|
|
|
58 |
<p id="230227" class="block-content">
|
|
|
59 |
<tt>TUrlMapping</tt> enables recognition of customized URL formats based on a list prespecified of URL patterns. Each pattern is specified in a <tt><url></tt> tag.
|
|
|
60 |
</p>
|
|
|
61 |
|
|
|
62 |
<p id="230228" class="block-content">
|
|
|
63 |
The <tt>Pattern</tt> and <tt>Parameters</tt> attribute
|
|
|
64 |
values are regular expression patterns that
|
|
|
65 |
determine the mapping criteria. The <tt>Pattern</tt> property takes
|
|
|
66 |
a regular expression with parameter names enclosed between a left brace '<tt>{</tt>'
|
|
|
67 |
and a right brace '<tt>}</tt>'. The patterns for each parameter can be set
|
|
|
68 |
using <tt>Parameters</tt> attribute collection.
|
|
|
69 |
For example,
|
|
|
70 |
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_230099">
|
|
|
71 |
<url ServiceParameter="ArticleView" pattern="articles/{year}/{month}/{day}"
|
|
|
72 |
parameters.year="\d{4}" parameters.month="\d{2}" parameters.day="\d+" />
|
|
|
73 |
</com:TTextHighlighter>
|
|
|
74 |
</p>
|
|
|
75 |
The example is equivalent to the following regular expression (it uses the "named group" feature in regular expressions available in PHP):
|
|
|
76 |
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_230100">
|
|
|
77 |
<url ServiceParameter="ArticleView"
|
|
|
78 |
RegularExpression="/articles\/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d+)/u" />
|
|
|
79 |
</com:TTextHighlighter>
|
|
|
80 |
<p id="230229" class="block-content">
|
|
|
81 |
In the above example, the pattern contains 3 parameters named "<tt>year</tt>",
|
|
|
82 |
"<tt>month</tt>" and "<tt>day</tt>". The pattern for these parameters are,
|
|
|
83 |
respectively, "<tt>\d{4}</tt>" (4 digits), "<tt>\d{2}</tt>" (2 digits)
|
|
|
84 |
and "<tt>\d+</tt>" (1 or more digits).
|
|
|
85 |
Essentially, the <tt>Parameters</tt> attribute name and values are used
|
|
|
86 |
as substrings in replacing the placeholders in the <tt>Pattern</tt> string
|
|
|
87 |
to form a complete regular expression string.
|
|
|
88 |
</p>
|
|
|
89 |
|
|
|
90 |
<div class="note"><b class="tip">Note:</b> If you intended to use the <tt>RegularExpression</tt>
|
|
|
91 |
property you need to escape the slash in regular expressions.
|
|
|
92 |
</div>
|
|
|
93 |
|
|
|
94 |
<p id="230230" class="block-content">Following from the above pattern example,
|
|
|
95 |
an URL <tt>http://example.com/index.php/articles/2006/07/21</tt> will be matched
|
|
|
96 |
and valid. However, <tt>http://example.com/index.php/articles/2006/07/hello</tt> is not
|
|
|
97 |
valid since the <tt>day</tt> parameter pattern is not satisfied.
|
|
|
98 |
In the default <tt>TUrlMappingPattern</tt> class, the pattern is matched against the
|
|
|
99 |
<b>PATH_INFO</b> part of the URL only. For example, only the <tt>/articles/2006/07/21</tt>
|
|
|
100 |
portion of the URL is considered.
|
|
|
101 |
</p>
|
|
|
102 |
|
|
|
103 |
<p id="230231" class="block-content">
|
|
|
104 |
The mapped request URL is equivalent to <tt>index.php?page=ArticleView&year=2006&month=07&day=21</tt>.
|
|
|
105 |
The request parameter values are available through the standard <tt>Request</tt>
|
|
|
106 |
object. For example, <tt>$this->Request['year']</tt>.
|
|
|
107 |
</p>
|
|
|
108 |
|
|
|
109 |
<p id="230232" class="block-content">The URL mapping are evaluated in order they are placed and only the first pattern that matches
|
|
|
110 |
the URL will be used. Cascaded mapping can be achieved by placing the URL mappings
|
|
|
111 |
in particular order. For example, placing the most specific mappings first.
|
|
|
112 |
</p>
|
|
|
113 |
|
|
|
114 |
<p class="block-content">Since version 3.1.4, Prado also provides wildcard patterns to use friendly URLs
|
|
|
115 |
for a bunch of pages in a directory with a single rule. Therefore you can use the <tt>{*}</tt> wildcard
|
|
|
116 |
in your pattern to let Prado know, where to find the ServiceID in your request URL. You can also specify
|
|
|
117 |
parameters with these patterns if a lot of pages share common parameters.</p>
|
|
|
118 |
|
|
|
119 |
<com:TTextHighlighter Language="xml" CssClass="source block-content">
|
|
|
120 |
<url ServiceParameter="Posts.*" pattern="posts/{*}/{id}" parameters.id="\d+" />
|
|
|
121 |
<url ServiceParameter="Posts.*" pattern="posts/{*}" />
|
|
|
122 |
<url ServiceParameter="Static.Info.*" pattern="info/{*}" />
|
|
|
123 |
</com:TTextHighlighter>
|
|
|
124 |
|
|
|
125 |
<p class="block-content">With these rules, any of the following URLs will be recognized:
|
|
|
126 |
<ul id="u1" class="block-content">
|
|
|
127 |
<li><tt>/index.php/post/ViewPost/123</tt> is recognized as <tt>/index.php?page=Posts.ViewPost&id=123</tt></li>
|
|
|
128 |
<li><tt>/index.php/post/ListPost/123</tt> is recognized as <tt>/index.php?page=Posts.ListPost&id=123</tt></li>
|
|
|
129 |
<li><tt>/index.php/post/ListPost/123</tt> is recognized as <tt>/index.php?page=Posts.ListPost&id=123</tt></li>
|
|
|
130 |
<li><tt>/index.php/post/MyPost</tt> is recognized as <tt>/index.php?page=Posts.MyPost</tt></li>
|
|
|
131 |
<li><tt>/index.php/info/Conditions</tt> is recognized as <tt>/index.php?page=Static.Info.Conditions</tt></li>
|
|
|
132 |
<li><tt>/index.php/info/About</tt> is recognized as <tt>/index.php?page=Static.Info.About</tt></li>
|
|
|
133 |
</ul>
|
|
|
134 |
</p>
|
|
|
135 |
|
|
|
136 |
<p>As above, put more specific rules before more common rules as the first matching rule will be used.</p>
|
|
|
137 |
|
|
|
138 |
<p>To make configuration of friendly URLs for multiple pages even easier, you can also use <tt>UrlFormat="Path"</tt>
|
|
|
139 |
in combination with wildcard patterns. In fact, this feature only is available in combination with wildcard rules:</P>
|
|
|
140 |
|
|
|
141 |
<com:TTextHighlighter Language="xml" CssClass="source block-content">
|
|
|
142 |
<url ServiceParameter="user.admin.*" pattern="admin/{*}" UrlFormat="Path"/>
|
|
|
143 |
<url ServiceParameter="*" pattern="{*}" UrlFormat="Path" />
|
|
|
144 |
</com:TTextHighlighter>
|
|
|
145 |
|
|
|
146 |
<p class="block-content">Parameters will get appended to the specified patterns as name/value pairs, separated by a "/".
|
|
|
147 |
(You can change the separator character with <tt>UrlParamSeparator</tt>.)
|
|
|
148 |
<ul id="u1" class="block-content">
|
|
|
149 |
<li><tt>/index.php/list/cat/15/month/12</tt> is recognized as <tt>/index.php?page=list&cat=15&month=12</tt></li>
|
|
|
150 |
<li><tt>/index.php/edit/id/12</tt> is recognized as <tt>/index.php?page=list&id=12</tt></li>
|
|
|
151 |
<li><tt>/index.php/show/name/foo</tt> is recognized as <tt>/index.php?page=show&name=foo</tt></li>
|
|
|
152 |
<li><tt>/index.php/admin/edit/id/12</tt> is recognized as <tt>/index.php?page=user.admin.edit&id=12</tt></li>
|
|
|
153 |
</ul>
|
|
|
154 |
</p>
|
|
|
155 |
|
|
|
156 |
<h2 id="46024">Constructing Customized URLs</h2>
|
|
|
157 |
<p id="230233" class="block-content">
|
|
|
158 |
Since version 3.1.1, <tt>TUrlMapping</tt> starts to support constructing customized URLs based on the provided patterns. To enable this feature, set <tt>TUrlMapping.EnableCustomUrl</tt> to true. When <tt>THttpRequest.constrcutUrl()</tt> is invoked, the actual URL construction work will be delegated to a matching <tt>TUrlMappingPattern</tt> instance. It replaces the parameters in the pattern with the corresponding GET variables passed to <tt>constructUrl()</tt>.
|
|
|
159 |
</p>
|
|
|
160 |
|
|
|
161 |
<p id="230234" class="block-content">
|
|
|
162 |
A matching pattern is one whose <tt>ServiceID</tt> and <tt>ServiceParameter</tt> properties are the same as those passed to <tt>constructUrl()</tt> and whose named parameters are found in the GET variables. For example, <tt>constructUrl('Posts.ListPost',array('cat'=>2))</tt> will use the third pattern in the above example.
|
|
|
163 |
<p>
|
|
|
164 |
|
|
|
165 |
<p id="230235" class="block-content">
|
|
|
166 |
By default, <tt>TUrlMapping</tt> will construct URLs prefixed with the currently requesting PHP script path, such as <tt><b>/path/to/index.php</b>/article/3</tt>. Users may change this behavior by explicitly specifying the URL prefix through its <tt>UrlPrefix</tt> property. For example, if the Web server configuration treats <tt>index.php</tt> as the default script, we can set <tt>UrlPrefix</tt> as <tt>/path/to</tt> and the constructed URL will look like <tt>/path/to/article/3</tt>.
|
|
|
167 |
</p>
|
|
|
168 |
|
|
|
169 |
<div class="note"><b class="tip">Note:</b> If you use <tt>constructUrl()</tt> with string parameters that contain slashes ("/") they will get encoded to %2F. By default most Apache installations give a "404 Not found" if a URL contains a %2F. You can add <tt>AllowEncodedSlashes On</tt> to your <tt>VirtualHost</tt> configuration to resolve this. (Available since Apache 2.0.46).
|
|
|
170 |
</div>
|
|
|
171 |
|
|
|
172 |
<div class="last-modified">$Id: UrlMapping.page 2601 2009-01-09 09:55:01Z haertl.mike $</div></com:TContent>
|