mirror of https://github.com/flightphp/core
parent
b1ed70e0fa
commit
b05959fa86
@ -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
|
||||
});
|
Loading…
Reference in new issue