From 219649556c0337327d6afa826edd47067b981a2f Mon Sep 17 00:00:00 2001 From: shoully Date: Sat, 20 Dec 2014 06:59:49 -0800 Subject: [PATCH] Updated 03 Routing (markdown) --- 03 Routing.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/03 Routing.md b/03 Routing.md index 5fc9cb5..6fba5c9 100644 --- a/03 Routing.md +++ b/03 Routing.md @@ -68,6 +68,21 @@ You can also include regular expressions with your named parameters by using the // But will not match /bob/12345 }); +### Optional Parameters + +You can specify named parameters that are optional for matching by wrapping segments in parentheses. + + + Flight::route('/blog(/@year(/@month(/@day)))', function($year, $month, $day){ + // This will match the following URLS: + // /blog/2012/12/10 + // /blog/2012/12 + // /blog/2012 + // /blog + }); + +Any optional parameters that are not matched will be passed in as NULL. + ### Wildcards Matching is only done on individual URL segments. If you want to match multiple segments you can use the `*` wildcard. @@ -80,4 +95,39 @@ To route all requests to a single callback, you can do: Flight::route('*', function(){ // Do something - }); \ No newline at end of file + }); + +### Passing + +You can pass execution on to the next matching route by returning true from your callback function. + + Flight::route('/user/@name', function($name){ + // Check some condition + if ($name != "Bob") { + // Continue to next route + return true; + } + }); + + Flight::route('/user/*', function(){ + // This will get called + }); + +### Route Info + +If you want to inspect the matching route information, you can request for the route object to be passed to your callback by passing in true as the third parameter in the route method. The route object will always be the last parameter passed to your callback function. + + + Flight::route('/', function($route){ + // Array of HTTP methods matched against + $route->methods; + + // Array of named parameters + $route->params; + + // Matching regular expression + $route->regex; + + // Contains the contents of any '*' used in the URL pattern + $route->splat; + }, true);