From b05959fa86c37fceda9a24cd7f620be9f769b804 Mon Sep 17 00:00:00 2001 From: shoully Date: Sat, 20 Dec 2014 05:56:48 -0800 Subject: [PATCH] Created Routing (markdown) --- Routing.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Routing.md diff --git a/Routing.md b/Routing.md new file mode 100644 index 0000000..27b40cb --- /dev/null +++ b/Routing.md @@ -0,0 +1,83 @@ +Routing in Flight is done by matching a URL pattern with a callback function. + + Flight::route('/', function(){ + echo 'hello world!'; + }); + +The callback can be any object that is callable. So you can use a regular function: + + function hello(){ + echo 'hello world!'; + } + + Flight::route('/', 'hello'); + +Or a class method: + + class Greeting { + public static function hello() { + echo 'hello world!'; + } + } + + Flight::route('/', array('Greeting','hello')); + +Routes are matched in the order they are defined. The first route to match a request will be invoked. + +## Method Routing + +By default, route patterns are matched against all request methods. You can respond to specific +methods by placing an identifier before the URL. + + Flight::route('GET /', function(){ + echo 'I received a GET request.'; + }); + + Flight::route('POST /', function(){ + echo 'I received a POST request.'; + }); + +You can also map multiple methods to a single callback by using a `|` delimiter: + + Flight::route('GET|POST /', function(){ + echo 'I received either a GET or a POST request.'; + }); + +Method specific routes have precedence over global routes. + +## Regular Expressions + +You can use regular expressions in your routes: + + Flight::route('/user/[0-9]+', function(){ + // This will match /user/1234 + }); + +## Named Parameters + +You can specify named parameters in your routes which will be passed along to your callback function. + + Flight::route('/@name/@id', function($name, $id){ + echo "hello, $name ($id)!"; + }); + +You can also include regular expressions with your named parameters by using the `:` delimiter: + + Flight::route('/@name/@id:[0-9]{3}', function($name, $id){ + // This will match /bob/123 + // But will not match /bob/12345 + }); + +## Wildcards + +Matching is only done on individual URL segments. If you want to match multiple segments you can use the `*` wildcard. + + Flight::route('/blog/*', function(){ + // This will match /blog/2000/02/01 + }); + +To route all requests to a single callback, you can do: + + Flight::route('*', function(){ + // Do something + }); \ No newline at end of file