1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-10 18:35:20 +02:00

Clean-up of mapping routes doc

This commit is contained in:
Danny van Kooten
2014-10-07 17:58:12 +02:00
parent 91ca1a68ff
commit 5d4de274a8

View File

@@ -16,18 +16,19 @@ layout: default
<p>The <code>map()</code> method accepts the following parameters.</p>
<h3><code>$method</code> (string)</h3>
<p>
<code>$method</code> (string)<br />
This is a pipe-delimited string of the accepted HTTP requests methods.<br /><br />
<em>Example: </em><code>GET|POST|PATCH|PUT|DELETE</code>
</p>
<h3><code>$route</code> (string)</h3>
<p>
<code>$route</code> (string)<br />
This is the route pattern to match against. This can be a plain string, one of the predefined regex filters or a custom regex. Custom regexes must start with <code>@</code>.
<br /><br />
<em>Examples: </em>
</p>
<table>
<tr>
<th>Route</th>
@@ -51,22 +52,22 @@ layout: default
</tr>
</table>
<h3><code>$target</code> (mixed) </h3>
<p>As AltoRouter leaves handling routes up to you, this can be anything.</p>
<p>
<code>$target</code> (mixed)<br />
As AltoRouter leaves handling routes up to you, this can be anything.<br /><br />
<em>Example using a function callback:</em> <br />
<code>function() { ... }</code>
</p>
<p>
<br /><br />
<em>Example using a controller#action string:</em> <br />
<code>UserController#showDetails</code>
</p>
<h3><code>$name</code> (string, optional)</h3>
<p>If you want to use reversed routing, specify a name parameter so you can later generate URL's using this route.</p>
<p>
<code>$name</code> (string, optional)<br />
If you want to use reversed routing, specify a name parameter so you can later generate URL's using this route.<br /><br />
<em>Example:</em><br />
<code>user_details</code></coe>
<code>user_details</code>
</p>
<h3>Example Mapping</h3>
@@ -94,33 +95,36 @@ $router->map( 'POST', '/contact/', 'handleContactForm' );
<h2>Match Types</h2>
<p>You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.</p>
<div class="highlight highlight-php"><pre><span class="o">*</span> <span class="c1">// Match all request URIs</span>
<span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="c1">// Match an integer</span>
<span class="p">[</span><span class="nx">i</span><span class="o">:</span><span class="nx">id</span><span class="p">]</span> <span class="c1">// Match an integer as 'id'</span>
<span class="p">[</span><span class="nx">a</span><span class="o">:</span><span class="nx">action</span><span class="p">]</span> <span class="c1">// Match alphanumeric characters as 'action'</span>
<span class="p">[</span><span class="nx">h</span><span class="o">:</span><span class="nb">key</span><span class="p">]</span> <span class="c1">// Match hexadecimal characters as 'key'</span>
<span class="p">[</span><span class="o">:</span><span class="nx">action</span><span class="p">]</span> <span class="c1">// Match anything up to the next / or end of the URI as 'action'</span>
<span class="p">[</span><span class="nx">create</span><span class="o">|</span><span class="nx">edit</span><span class="o">:</span><span class="nx">action</span><span class="p">]</span> <span class="c1">// Match either 'create' or 'edit' as 'action'</span>
<span class="p">[</span><span class="o">*</span><span class="p">]</span> <span class="c1">// Catch all (lazy, stops at the next trailing slash)</span>
<span class="p">[</span><span class="o">*:</span><span class="nx">trailing</span><span class="p">]</span> <span class="c1">// Catch all as 'trailing' (lazy)</span>
<span class="p">[</span><span class="o">**:</span><span class="nx">trailing</span><span class="p">]</span> <span class="c1">// Catch all (possessive - will match the rest of the URI)</span>
<span class="o">.</span><span class="p">[</span><span class="o">:</span><span class="nx">format</span><span class="p">]</span><span class="o">?</span> <span class="c1">// Match an optional parameter 'format' - a / or . before the block is also optional</span>
</pre></div>
{% highlight php startinline %}
* // Match all request URIs
[i] // Match an integer
[i:id] // Match an integer as 'id'
[a:action] // Match alphanumeric characters as 'action'
[h:key] // Match hexadecimal characters as 'key'
[:action] // Match anything up to the next / or end of the URI as 'action'
[create|edit:action] // Match either 'create' or 'edit' as 'action'
[*] // Catch all (lazy, stops at the next trailing slash)
[*:trailing] // Catch all as 'trailing' (lazy)
[**:trailing] // Catch all (possessive - will match the rest of the URI)
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional
{% endhighlight %}
<p>The character before the colon (the 'match type') is a shortcut for one of the following regular expressions</p>
<div class="highlight highlight-php"><pre><span class="s1">'i'</span> <span class="o">=&gt;</span> <span class="s1">'[0-9]++'</span>
<span class="s1">'a'</span> <span class="o">=&gt;</span> <span class="s1">'[0-9A-Za-z]++'</span>
<span class="s1">'h'</span> <span class="o">=&gt;</span> <span class="s1">'[0-9A-Fa-f]++'</span>
<span class="s1">'*'</span> <span class="o">=&gt;</span> <span class="s1">'.+?'</span>
<span class="s1">'**'</span> <span class="o">=&gt;</span> <span class="s1">'.++'</span>
<span class="s1">''</span> <span class="o">=&gt;</span> <span class="s1">'[^/\.]++'</span>
</pre></div>
{% highlight php startinline %}
'i' => '[0-9]++'
'a' => '[0-9A-Za-z]++'
'h' => '[0-9A-Fa-f]++'
'*' => '.+?'
'**' => '.++'
'' => '[^/\.]++'
{% endhighlight %}
<p>You can register your own match types using the <code>addMatchTypes()</code> method.</p>
<div class="highlight highlight-php"><pre><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">addMatchTypes</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'cId'</span> <span class="o">=&gt;</span> <span class="s1">'[a-zA-Z]{2}[0-9](?:_[0-9]++)?'</span><span class="p">));</span>
</pre></div>
{% highlight php startinline %}
$router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
{% endhighlight %}
<p>Once your routes are all mapped you can start matching requests and continue processing the request.</p>