-
Notifications
You must be signed in to change notification settings - Fork 4
Routes
Routing is where all of Tessera’s power comes from. Routes basically define what URL patterns are mapped to which methods in your application class.
<?php
require '../tessera.php';
class ArguableApp extends Tessera {
function foo($bar) {
echo "Bar is {$bar}, and that's that";
}
}
$basic = new ArguableApp(array(
'/foo/$bar' => 'foo'
));
For the rest of this writeup, assume that the code is saved as arguable.php on your domain, tesseratic.com.
In the ArguableApp application, only one route is defined. The route /foo/$bar is mapped to the ArguableApp::foo
method. The $bar part of the route is a variable, meaning that any call to /foo/* will be matched, and the matched text will be passed as a parameter to the ArguableApp::foo
method. For example, tesseratic.com/arguable.php?/foo/xyzzy will match, and $bar
inside of ArguableApp::foo
will equal "xyzzy"
.
Internally, routes are compiled to regular expressions, and parameter variables are compiled to (\w+), so keep that in mind when naming them. Routes can also be considerably more complex. If you add a new route to the application like this:
$basic = new ArguableApp(array(
'/foo/$bar' => 'foo',
'/bar/$baz/zip/$zap' => 'bar'
));
And the associated method:
function bar($baz, $zip) {
echo "Now we're getting nutty with {$baz} and {$zip}.";
}
Requests to tesseratic.com/arguable.php?/bar/squirtle/zip/charmander will match, and $baz
will equal "squirtle"
and $zip
will equal "charmander"
.
While reading this, you probably noticed and wondered: “Why are all my routes in the query string? That’s terribly ugly.” That’s because I want the distribution to be server-agnostic. It would be trivial to add some rewrite rules to your server to hide away the php extensions and query string marker. In fact, it’s all on the deployment page.