1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-16 13:24:09 +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> <p>The <code>map()</code> method accepts the following parameters.</p>
<h3><code>$method</code> (string)</h3>
<p> <p>
<code>$method</code> (string)<br />
This is a pipe-delimited string of the accepted HTTP requests methods.<br /><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> <em>Example: </em><code>GET|POST|PATCH|PUT|DELETE</code>
</p> </p>
<h3><code>$route</code> (string)</h3>
<p> <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>. 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 /> <br /><br />
<em>Examples: </em> <em>Examples: </em>
</p> </p>
<table> <table>
<tr> <tr>
<th>Route</th> <th>Route</th>
@@ -51,22 +52,22 @@ layout: default
</tr> </tr>
</table> </table>
<h3><code>$target</code> (mixed) </h3>
<p>As AltoRouter leaves handling routes up to you, this can be anything.</p>
<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 /> <em>Example using a function callback:</em> <br />
<code>function() { ... }</code> <code>function() { ... }</code>
</p> <br /><br />
<p>
<em>Example using a controller#action string:</em> <br /> <em>Example using a controller#action string:</em> <br />
<code>UserController#showDetails</code> <code>UserController#showDetails</code>
</p> </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> <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 /> <em>Example:</em><br />
<code>user_details</code></coe> <code>user_details</code>
</p> </p>
<h3>Example Mapping</h3> <h3>Example Mapping</h3>
@@ -94,33 +95,36 @@ $router->map( 'POST', '/contact/', 'handleContactForm' );
<h2>Match Types</h2> <h2>Match Types</h2>
<p>You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.</p> <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> {% highlight php startinline %}
<span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="c1">// Match an integer</span> * // Match all request URIs
<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> [i] // Match an integer
<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> [i:id] // Match an integer as 'id'
<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> [a:action] // Match alphanumeric characters as 'action'
<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> [h:key] // Match hexadecimal characters as 'key'
<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> [:action] // Match anything up to the next / or end of the URI as 'action'
<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> [create|edit:action] // Match either 'create' or 'edit' as 'action'
<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> [*] // Catch all (lazy, stops at the next trailing slash)
<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> [*:trailing] // Catch all as 'trailing' (lazy)
<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> [**:trailing] // Catch all (possessive - will match the rest of the URI)
</pre></div> .[: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> <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> {% highlight php startinline %}
<span class="s1">'a'</span> <span class="o">=&gt;</span> <span class="s1">'[0-9A-Za-z]++'</span> 'i' => '[0-9]++'
<span class="s1">'h'</span> <span class="o">=&gt;</span> <span class="s1">'[0-9A-Fa-f]++'</span> 'a' => '[0-9A-Za-z]++'
<span class="s1">'*'</span> <span class="o">=&gt;</span> <span class="s1">'.+?'</span> 'h' => '[0-9A-Fa-f]++'
<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> '' => '[^/\.]++'
{% endhighlight %}
<p>You can register your own match types using the <code>addMatchTypes()</code> method.</p> <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> {% highlight php startinline %}
</pre></div> $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> <p>Once your routes are all mapped you can start matching requests and continue processing the request.</p>