1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-07-30 21:20:20 +02:00

Add usage pages.

This commit is contained in:
Danny van Kooten
2014-10-07 17:53:39 +02:00
parent b2b5242375
commit 91ca1a68ff
10 changed files with 486 additions and 208 deletions

View File

@@ -6,7 +6,6 @@
<title>{{ page.title }}</title>
<link rel="stylesheet" href="/stylesheets/styles.css">
<link rel="stylesheet" href="/stylesheets/pygment_trac.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
@@ -21,28 +20,29 @@
ga('send', 'pageview');
</script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<header>
<h1>Altorouter</h1>
<h1 class="site-title"><a href="/">Altorouter</a></h1>
<p>PHP5.3+ Routing Class. Supports REST, dynamic and reversed routing. </p>
<p class="view"><a href="https://github.com/dannyvankooten/AltoRouter">View the Project on GitHub <small>dannyvankooten/AltoRouter</small></a></p>
<a href="https://github.com/dannyvankooten/AltoRouter/zipball/master" rel="nofollow" class="btn"><i class="fa fa-download"></i> Download ZIP</a>
<a href="https://github.com/dannyvankooten/AltoRouter" class="btn"><i class="fa fa-github-alt"></i> View on GitHub</a>
<ul>
<li><a href="https://github.com/dannyvankooten/AltoRouter/zipball/master">Download <strong>ZIP File</strong></a></li>
<li><a href="https://github.com/dannyvankooten/AltoRouter/tarball/master">Download <strong>TAR Ball</strong></a></li>
<li><a href="https://github.com/dannyvankooten/AltoRouter">View On <strong>GitHub</strong></a></li>
</ul>
</header>
<section>
{{ content }}
</section>
<footer>
<iframe src="/github-stars.html?user=dannyvankooten&amp;repo=altorouter&amp;type=watch&amp;count=true" allowtransparency="true" frameborder="0" scrolling="0" width="120px" height="20px" style="margin-bottom: 20px;"></iframe>
<p>This project is maintained by <a href="https://github.com/dannyvankooten">dannyvankooten</a></p>
<p><small>Hosted on GitHub Pages &mdash; Theme by <a rel="nofollow" href="https://github.com/orderedlist">orderedlist</a></small></p>
<p><small>Hosted on GitHub Pages<br />Theme by <a rel="nofollow" href="https://github.com/orderedlist">orderedlist</a></small></p>
</footer>
</div>
<script src="/javascripts/scale.fix.js"></script>

1
github-stars.html Normal file

File diff suppressed because one or more lines are too long

View File

@@ -2,150 +2,46 @@
title: AltoRouter - PHP Router that supports REST, dynamic and reversed routing
layout: default
---
<h1>
<h1>
<a name="altorouter-" class="anchor" href="#altorouter-"><span class="octicon octicon-link"></span></a>AltoRouter <a href="http://travis-ci.org/dannyvankooten/AltoRouter"><img src="https://api.travis-ci.org/dannyvankooten/AltoRouter.png" alt="Build Status"></a>
</h1>
<p>AltoRouter is a small but powerful routing class for PHP 5.3+, heavily inspired by <a href="https://github.com/chriso/klein.php/">klein.php</a>.</p>
<p class="intro">AltoRouter is a small but powerful routing class for PHP 5.3+, heavily inspired by <a href="https://github.com/chriso/klein.php/">klein.php</a>.</p>
{% highlight php startinline %}
$router = new AltoRouter();
// map homepage
$router->map( 'GET', '/', function() {
require __DIR__ . '/views/home.php';
});
// map users details page
$router->map( 'GET|POST', '/users/[i:id]/', function( $id ) {
$user = .....
require __DIR__ . '/views/user/details.php';
});
{% endhighlight %}
<h2>Router Features</h2>
<ul>
<li>Dynamic routing with named parameters</li>
<li>Reversed routing</li>
<li>Flexible regular expression routing (inspired by <a href="http://www.sinatrarb.com/">Sinatra</a>)</li>
<li>Custom regexes</li>
<li>HTTP methods</li>
<li>Dynamic routing with (named) route parameters</li>
<li>Flexible regular expression routing, inspired by <a href="http://www.sinatrarb.com/">Sinatra</a></li>
<li>Reversed routing</li>
<li>Custom regexes</li>
</ul>
<h2>
<a name="getting-started" class="anchor" href="#getting-started"><span class="octicon octicon-link"></span></a>
Getting started
</h2>
<h2>Using AltoRouter</h2>
<p>To use AltoRouter in your project you need a web server running PHP 5.3 or newer.</p>
<ol>
<li>PHP 5.3.x is required</li>
<li>Install AltoRouter using Composer or manually</li>
<li>Setup URL rewriting so that all requests are handled by <strong>index.php</strong>
</li>
<li>Create an instance of AltoRouter, map your routes and match a request.</li>
<li>Have a look at the basic example in the <code>examples</code> directory for a better understanding on how to use AltoRouter.</li>
</ol><h2>
<a name="user-content-routing" class="anchor" href="#routing" aria-hidden="true"><span class="octicon octicon-link"></span></a>Routing</h2>
<p>With AltoRouter you define new routes by using the <code>map</code> method.</p>
<p><code>map</code> accepts 4 parameters;</p>
<p><strong>Request methods</strong><br>
One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)</p>
<p><strong>Route pattern</strong><br>
Route pattern to match with. You can use multiple pre-set regex filters, like <code>[i:id]</code>. A custom regex must start with an <code>@</code>.</p>
<p><strong>Target</strong><br>
The target of where this route should point to. Can be anything.</p>
<p><strong>Name</strong><br>
Optional name of this route. Supply if you want to reverse route this url in your application.</p>
<h3>
<a name="user-content-example" class="anchor" href="#example" aria-hidden="true"><span class="octicon octicon-link"></span></a>Example</h3>
<div class="highlight highlight-php"><pre><span class="nv">$router</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AltoRouter</span><span class="p">();</span>
<span class="c1">// (optional) set basepath to the subdirectory relative to the application root</span>
<span class="c1">// or, a virtual path where you want to integrate AltoRouter.</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">setBasePath</span><span class="p">(</span><span class="s1">'/AltoRouter'</span><span class="p">);</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">'GET|POST'</span><span class="p">,</span><span class="s1">'/'</span><span class="p">,</span> <span class="s1">'home#index'</span><span class="p">,</span> <span class="s1">'home'</span><span class="p">);</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span><span class="s1">'/users'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'c'</span> <span class="o">=&gt;</span> <span class="s1">'UserController'</span><span class="p">,</span> <span class="s1">'a'</span> <span class="o">=&gt;</span> <span class="s1">'ListAction'</span><span class="p">));</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span><span class="s1">'/users/[i:id]'</span><span class="p">,</span> <span class="s1">'users#show'</span><span class="p">,</span> <span class="s1">'users_show'</span><span class="p">);</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">'POST'</span><span class="p">,</span><span class="s1">'/users/[i:id]/[delete|update:action]'</span><span class="p">,</span> <span class="s1">'usersController#doAction'</span><span class="p">,</span> <span class="s1">'users_do'</span><span class="p">);</span>
<span class="c1">// reversed routing</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">generate</span><span class="p">(</span><span class="s1">'users_show'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'id'</span> <span class="o">=&gt;</span> <span class="mi">5</span><span class="p">));</span> <span class="c1"># =&gt; /users/5</span>
</pre></div>
<p>For quickly adding multiple routes, you can use the <code>addRoutes</code> method. This method accepts an array or any kind of traversable.</p>
<div class="highlight highlight-php"><pre><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">addRoutes</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'PATCH'</span><span class="p">,</span><span class="s1">'/users/[i:id]'</span><span class="p">,</span> <span class="s1">'users#update'</span><span class="p">,</span> <span class="s1">'update_user'</span><span class="p">),</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'DELETE'</span><span class="p">,</span><span class="s1">'/users/[i:id]'</span><span class="p">,</span> <span class="s1">'users#delete'</span><span class="p">,</span> <span class="s1">'delete_user'</span><span class="p">)</span>
<span class="p">));</span>
</pre></div>
<p><strong>You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.</strong></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>
<p><strong>Some more complicated examples</strong></p>
<div class="highlight highlight-php"><pre><span class="o">@/</span><span class="p">(</span><span class="o">?</span><span class="p">[</span><span class="nx">A</span><span class="o">-</span><span class="nx">Za</span><span class="o">-</span><span class="nx">z</span><span class="p">]{</span><span class="mi">2</span><span class="p">}</span><span class="nx">_</span><span class="p">[</span><span class="nx">A</span><span class="o">-</span><span class="nx">Za</span><span class="o">-</span><span class="nx">z</span><span class="p">]{</span><span class="mi">2</span><span class="p">})</span><span class="err">$</span> <span class="c1">// custom regex, matches language codes like "en_us" etc.</span>
<span class="o">/</span><span class="nx">posts</span><span class="o">/</span><span class="p">[</span><span class="o">*:</span><span class="nx">title</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">// Matches "/posts/this-is-a-title-123"</span>
<span class="o">/</span><span class="nx">output</span><span class="o">.</span><span class="p">[</span><span class="nx">xml</span><span class="o">|</span><span class="nx">json</span><span class="o">:</span><span class="nx">format</span><span class="p">]</span><span class="o">?</span> <span class="c1">// Matches "/output", "output.xml", "output.json"</span>
<span class="o">/</span><span class="p">[</span><span class="o">:</span><span class="nx">controller</span><span class="p">]</span><span class="o">?/</span><span class="p">[</span><span class="o">:</span><span class="nx">action</span><span class="p">]</span><span class="o">?</span> <span class="c1">// Matches the typical /controller/action format</span>
</pre></div>
<p><strong>The character before the colon (the 'match type') is a shortcut for one of the following regular expressions</strong></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>
<p><strong>New match types can be added using the <code>addMatchTypes()</code> method</strong></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>
<h2>
<a name="user-content-matching" class="anchor" href="#matching" aria-hidden="true"><span class="octicon octicon-link"></span></a>Matching</h2>
<p>Route lookup is done by calling <code>match</code>.</p>
<div class="highlight highlight-php"><pre><span class="c1">// perform a match against the current request url</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">match</span><span class="p">();</span>
<span class="c1">// perform a match against a given url</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">match</span><span class="p">(</span><span class="s1">'/path/to/somewhere'</span><span class="p">);</span>
</pre></div>
<h3>
<a name="user-content-return-value" class="anchor" href="#return-value" aria-hidden="true"><span class="octicon octicon-link"></span></a>Return value</h3>
<p>The return value will be an associative array if a match is found and <code>false</code> otherwise.</p>
<p>The array consists of 3 keys; <code>target</code>, <code>params</code> and <code>name</code>.</p>
<p>Where <code>target</code> and <code>name</code> contain the values passed to <code>map</code> and <code>params</code> is an associative array of params extracted from the url.</p>
<h3>
<a name="user-content-example-1" class="anchor" href="#example-1" aria-hidden="true"><span class="octicon octicon-link"></span></a>Example</h3>
<div class="highlight highlight-php"><pre><span class="nv">$router</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AltoRouter</span><span class="p">();</span>
<span class="nv">$router</span><span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">'/user/[i:id]'</span><span class="p">,</span> <span class="s1">'user_controller#show_profile'</span><span class="p">,</span> <span class="s1">'user_profile'</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$router</span><span class="o">-&gt;</span><span class="na">match</span><span class="p">(</span><span class="s1">'/user/123'</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">==</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'target'</span> <span class="o">=&gt;</span> <span class="s1">'user_controller#show_profile'</span><span class="p">,</span>
<span class="s1">'params'</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'id'</span> <span class="o">=&gt;</span> <span class="mi">123</span>
<span class="p">),</span>
<span class="s1">'name'</span> <span class="o">=&gt;</span> <span class="s1">'user_profile'</span>
<span class="p">);</span>
</pre>
<ul>
<li><a href="/usage/install.html">Install AltoRouter</a></li>
<li><a href="/usage/rewrite-requests.html">Rewrite all requests to AltoRouter</a></li>
<li><a href="/usage/mapping-routes.html">Map your routes</a></li>
<li><a href="/usage/matching-requests.html">Match requests</a></li>
<li><a href="/usage/processing-requests.html">Process the request your preferred way</a></li>
</ul>
<h2>
<a name="contributors" class="anchor" href="#contributors"><span class="octicon octicon-link"></span></a>Contributors</h2>
@@ -155,15 +51,8 @@ layout: default
<li><a href="https://github.com/koenpunt">Koen Punt</a></li>
<li><a href="https://github.com/adduc">John Long</a></li>
<li><a href="https://github.com/niahoo">Niahoo Osef</a></li>
</ul><h2>
<a name="license" class="anchor" href="#license"><span class="octicon octicon-link"></span></a>License</h2>
</ul>
<p>(MIT License)</p>
<p>Copyright (c) 2012-2014 Danny van Kooten <a href="mailto:hi@dannyvankooten.com">hi@dannyvankooten.com</a></p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<p>Released under the <a href="/license.html">MIT Public License</a>.</p>
<p>&copy; 2012-2014 <a href="https://dannyvankooten.com/">Danny van Kooten</a></p>

15
license.html Normal file
View File

@@ -0,0 +1,15 @@
---
title: License - AltoRouter
layout: default
---
<h1>License</h1>
<p>AltoRouter is released under the MIT Public License.</p>
<p>Copyright (c) 2012-2014 <a href="https://dannyvankooten.com/">Danny van Kooten</a><br />
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>

View File

@@ -24,6 +24,12 @@ h1 {
font-size:28px;
}
.site-title a{
text-decoration: none;
color: inherit;
font-weight: inherit;
}
h2 {
color:#393939;
}
@@ -32,6 +38,10 @@ h3, h4, h5, h6 {
color:#494949;
}
.subtitle {
margin-top: 40px;
}
a {
color:#39c;
font-weight:400;
@@ -41,7 +51,6 @@ a {
a small {
font-size:11px;
color:#777;
margin-top:-0.6em;
display:block;
}
@@ -101,64 +110,11 @@ header {
position:fixed;
}
header ul {
list-style:none;
height:40px;
padding:0;
background: #eee;
background: -moz-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8f8f8), color-stop(100%,#dddddd));
background: -webkit-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: -o-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: -ms-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
border-radius:5px;
border:1px solid #d2d2d2;
box-shadow:inset #fff 0 1px 0, inset rgba(0,0,0,0.03) 0 -1px 0;
width:270px;
}
header li {
width:89px;
float:left;
border-right:1px solid #d2d2d2;
height:40px;
}
header ul a {
line-height:1;
font-size:11px;
color:#999;
display:block;
text-align:center;
padding-top:6px;
height:40px;
}
strong {
color:#222;
font-weight:700;
}
header ul li + li {
width:88px;
border-left:1px solid #fff;
}
header ul li + li + li {
border-right:none;
width:89px;
}
header ul a strong {
font-size:14px;
display:block;
color:#222;
}
section {
width:560px;
float:right;
@@ -183,6 +139,29 @@ footer {
bottom:50px;
}
h1 small {
color: lightGray;
font-size: 60%;
display: block;
margin-bottom: 10px;
}
.btn {
display: inline-block;
white-space: nowrap;
margin-bottom: 10px;
font-size: 14px;
background: #39c;
padding: 5px 10px;
color: white;
border-bottom: 2px solid #006699;
}
.btn:hover {
background: #006699;
border-bottom-color: #003366;
}
@media print, screen and (max-width: 960px) {
div.wrapper {
@@ -253,3 +232,74 @@ footer {
color:#444;
}
}
/* pygments */
.highlight { background: #ffffff; }
.highlight .c { color: #999988; font-style: italic } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { font-weight: bold } /* Keyword */
.highlight .o { font-weight: bold } /* Operator */
.highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
.highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
.highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #999999 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold; } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { font-weight: bold } /* Keyword.Constant */
.highlight .kd { font-weight: bold } /* Keyword.Declaration */
.highlight .kn { font-weight: bold } /* Keyword.Namespace */
.highlight .kp { font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #009999 } /* Literal.Number */
.highlight .s { color: #d14 } /* Literal.String */
.highlight .na { color: #008080 } /* Name.Attribute */
.highlight .nb { color: #0086B3 } /* Name.Builtin */
.highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
.highlight .no { color: #008080 } /* Name.Constant */
.highlight .ni { color: #800080 } /* Name.Entity */
.highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
.highlight .nn { color: #555555 } /* Name.Namespace */
.highlight .nt { color: #000080 } /* Name.Tag */
.highlight .nv { color: #008080 } /* Name.Variable */
.highlight .ow { font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #009999 } /* Literal.Number.Float */
.highlight .mh { color: #009999 } /* Literal.Number.Hex */
.highlight .mi { color: #009999 } /* Literal.Number.Integer */
.highlight .mo { color: #009999 } /* Literal.Number.Oct */
.highlight .sb { color: #d14 } /* Literal.String.Backtick */
.highlight .sc { color: #d14 } /* Literal.String.Char */
.highlight .sd { color: #d14 } /* Literal.String.Doc */
.highlight .s2 { color: #d14 } /* Literal.String.Double */
.highlight .se { color: #d14 } /* Literal.String.Escape */
.highlight .sh { color: #d14 } /* Literal.String.Heredoc */
.highlight .si { color: #d14 } /* Literal.String.Interpol */
.highlight .sx { color: #d14 } /* Literal.String.Other */
.highlight .sr { color: #009926 } /* Literal.String.Regex */
.highlight .s1 { color: #d14 } /* Literal.String.Single */
.highlight .ss { color: #990073 } /* Literal.String.Symbol */
.highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #008080 } /* Name.Variable.Class */
.highlight .vg { color: #008080 } /* Name.Variable.Global */
.highlight .vi { color: #008080 } /* Name.Variable.Instance */
.highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
.type-csharp .highlight .k { color: #0000FF }
.type-csharp .highlight .kt { color: #0000FF }
.type-csharp .highlight .nf { color: #000000; font-weight: normal }
.type-csharp .highlight .nc { color: #2B91AF }
.type-csharp .highlight .nn { color: #000000 }
.type-csharp .highlight .s { color: #A31515 }
.type-csharp .highlight .sc { color: #A31515 }

52
usage/install.html Normal file
View File

@@ -0,0 +1,52 @@
---
title: Installing AltoRouter
layout: default
---
<h1>
<small>Using AltoRouter</small>
Installation
</h1>
<h2>Install AltoRouter using Composer</h2>
<p>To install using Composer, you will have to install Composer first.</p>
{% highlight bash %}
curl -s https://getcomposer.org/installer | php
{% endhighlight %}
<p>Create a <strong>composer.json</strong> file in your project root.</p>
{% highlight json %}
{
"require": {
"altorouter/altorouter": "1.1.0"
}
}
{% endhighlight %}
<p>Tell Composer to install the required dependencies.</p>
{% highlight bash %}
php composer.phar install
{% endhighlight %}
<p>If you want to use the autoloading provided by Composer, add the following line to your application file.</p>
{% highlight php startinline %}
require 'vendor/autoload.php';
{% endhighlight %}
<p>You are now ready to use the <code>AltoRouter</code> class. <a href="/usage/rewrite-requests.html">Rewrite all requests to your application file</a> and start <a href="/usage/mapping-routes.html">mapping your routes</a>. </p>
<h2 class="subtitle">Install AltoRouter manually</h2>
<p>Just <a href="https://github.com/dannyvankooten/AltoRouter/zipball/master">download the AltoRouter class</a> and require it in your application file.</p>
{% highlight php startinline %}
require 'AltoRouter.php';
{% endhighlight %}
<p>
<a style="float:right;" href="/usage/rewrite-requests.html">Rewrite requests &raquo;</a>
<br style="clear:both;">
</p>

131
usage/mapping-routes.html Normal file
View File

@@ -0,0 +1,131 @@
---
title: "Mapping routes using AltoRouter"
layout: default
---
<h1>
<small>Using AltoRouter</small>
Mapping Routes
</h1>
<p>By now, you should have <a href="/usage/rewrite-requests.html">rewritten al requests to be handled by a single file in which you created an AltoRouter instance.</a></p>
<p>To map your routes, use the <code>map()</code> method. The following example maps all <code>GET /</code> requests.</p>
{% highlight php startinline %}
$router->map( 'GET', '/', 'render_home', 'home' );
{% endhighlight %}
<p>The <code>map()</code> method accepts the following parameters.</p>
<h3><code>$method</code> (string)</h3>
<p>
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>
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>
<th>Example Match</th>
<th>Variables</th>
</tr>
<tr>
<td><code>/contact/</code></td>
<td><code>/contact/</code></td>
<td></td>
</tr>
<tr>
<td><code>/users/[i:id]/</code></td>
<td><code>/users/12/</code></td>
<td><code>$id: 12</code></td>
</tr>
<tr>
<td><code>/[a:c]/[a:a]?/[i:id]?</code></td>
<td><code>/controller/action/21</code></td>
<td><code>$c: "controller", $a: "action", $id: 21</code></td>
</tr>
</table>
<h3><code>$target</code> (mixed) </h3>
<p>As AltoRouter leaves handling routes up to you, this can be anything.</p>
<p>
<em>Example using a function callback:</em> <br />
<code>function() { ... }</code>
</p>
<p>
<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>
<em>Example:</em><br />
<code>user_details</code></coe>
</p>
<h3>Example Mapping</h3>
{% highlight php startinline%}
// map homepage using callable
$router->map( 'GET', '/', function() {
require __DIR__ . '/views/home.php';
});
// map users details page using controller#action string
$router->map( 'GET', '/users/[i:id]/', 'UserController#showDetails' );
// map contact form handler using function name string
$router->map( 'POST', '/contact/', 'handleContactForm' );
{% endhighlight %}
<p>For quickly adding multiple routes, you can use the <code>addRoutes</code> method. This method accepts an array or any kind of traversable.</p>
<div class="highlight highlight-php"><pre><span class="nv">$router</span><span class="o">-&gt;</span><span class="na">addRoutes</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'PATCH'</span><span class="p">,</span><span class="s1">'/users/[i:id]'</span><span class="p">,</span> <span class="s1">'users#update'</span><span class="p">,</span> <span class="s1">'update_user'</span><span class="p">),</span>
<span class="k">array</span><span class="p">(</span><span class="s1">'DELETE'</span><span class="p">,</span><span class="s1">'/users/[i:id]'</span><span class="p">,</span> <span class="s1">'users#delete'</span><span class="p">,</span> <span class="s1">'delete_user'</span><span class="p">)</span>
<span class="p">));</span>
</pre></div>
<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>
<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>
<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>
<p>Once your routes are all mapped you can start matching requests and continue processing the request.</p>
<p>
<a style="float:left;" href="/usage/rewrite-requests.html">&laquo; Rewriting requests</a>
<a style="float:right;" href="/usage/matching-requests.html">Matching requests &raquo;</a>
<br style="clear:both;">
</p>

View File

@@ -0,0 +1,57 @@
---
title: Matching requests using AltoRouter
layout: default
---
<h1>
<small>Using AltoRouter</small>
Matching Requests
</h1>
<p>To match the current request, just call the `match()` method without any parameters.</p>
{% highlight php startinline %}
$match = $router->match();
{% endhighlight %}
<p>If a match was found, the <code>match()</code> method will return an associative array with the following 3 keys.</p>
<p>
<code>target</code> (mixed)<br />
The target of the route, given during mapping the route.
</p>
<p>
<code>params</code> (array)<br />
Named parameters found in the request url.
</p>
<p>
<code>name</code> (string)<br />
The name of the route.
</p>
<p><em>Example</em></p>
{% highlight php startinline %}
$router = new AltoRouter();
$router->map( 'GET', '/', function() { .. }, 'home' );
// assuming current request url = '/'
$match = $router->match();
/*
array(3) {
["target"] => object(Closure)#2 (0) { }
["params"] => array(0) { }
["name"] => 'home'
}
*/
{% endhighlight %}
<p>AltoRouter does not process the request for you, you are free to use the method you prefer. Here is a simplified example <a href="/usage/processing-requests.html">how to process requests using closures</a> though.</p>
<p>
<a style="float:left;" href="/usage/mapping-routes.html">&laquo; Mapping routes</a>
<a style="float:right;" href="/usage/processing-requests.html">Processing requests &raquo;</a>
<br style="clear:both;">
</p>

View File

@@ -0,0 +1,40 @@
---
title: Processing requests using AltoRouter
layout: default
---
<h1>
<small>Using AltoRouter</small>
Processing Requests
</h1>
<p>AltoRouter does not process requests for you so you are free to use the method you prefer. To help you get started, here's a simplified example using closures.</p>
{% highlight php startinline %}
$router = new AltoRouter();
// map homepage
$router->map( 'GET', '/', function() {
require __DIR__ . '/views/home.php';
});
// map user details page
$router->map( 'GET', '/user/[i:id]/', function( $id ) {
require __DIR__ . '/views/user-details.php';
});
// match current request url
$match = $router->match();
// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
{% endhighlight %}
<p>
<a style="float:left;" href="/usage/matching-requests.html">&laquo; Matching requests</a>
<br style="clear:both;">
</p>

View File

@@ -0,0 +1,43 @@
---
title: "Rewrite all requests to AltoRouter"
layout: default
---
<h1>
<small>Using AltoRouter</small>
Rewrite all requests to AltoRouter
</h1>
<p>To use AltoRouter, you will need to rewrite all requests to a single file in which you create an instance of the AltoRouter class.</p>
<p>There are various ways to go about this, but here are examples for Apache and Nginx.</p>
<h4>Apache <code>.htaccess</code></h4>
{% highlight apache %}
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
{% endhighlight %}
<h4>Nginx <code>nginx.conf</code></h4>
{% highlight nginx %}
try_files $uri /index.php;
{% endhighlight %}
<p>All requests are now handled by the same file. In that file, you create an <code>AltoRouter</code> instance.</p>
{% highlight php startinline %}
$router = new AltoRouter();
{% endhighlight %}
<p>Optionally, if your project lives in a sub-folder of your web root you use the <code>setBasePath()</code> method to set a base path.</p>
{% highlight php startinline %}
$router->setBasePath('/alto-app/');
{% endhighlight %}
<p>You are now ready to start <a href="/usage/mapping-routes.html">mapping your routes</a>.</p>
<p>
<a style="float:left;" href="/usage/install.html">&laquo; Installing AltoRouter</a>
<a style="float:right;" href="/usage/mapping-routes.html">Mapping routes &raquo;</a>
<br style="clear:both;">
</p>