Flight - a small lightweight router for PHP
PHP
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Corvum
README.md
composer.json

README.md

#Flight A Lightweight Router for PHP

##Installation

###Install with Composer

If you're using Composer to manage dependencies, you can add Flight with it.

{
    "require": {
        "corvum/flight": "1.0.0"
    }
}

##Usage

###Setup

require('/vendor/autoload.php');
$r = new \Corvum\Flight();

###Get Routes

$r->get('/',function(){ echo "Home Page"; });

###Post Routes

$r->post('/',function(){ echo "Posted to the Home Page"; });

###Other REST Methods

Flight accepts any method type to route. This will allow you to add DELETE ($r->delete), PUT($r->put) or any others that you wish

###404 Pages

$r->notFound(function(){ echo "404 Page"; });

##Url Routing

URLs are matched based on the root of the domain (everything after the .tld) using regex. Variables do not need to be named explicitly the same into the function as they are passed by order.

//URL: /Foo/Bar
$r->get('/Foo/Bar', function(){ echo 'FooBar'; }); //Prints 'FooBar'

//URL: /Foo/Bars
$r->get('/Foo/:bar', function($bar){ echo $bar; }); //Prints 'FooBars'

//URL: /Foo/Barred
$r->get('/:foo/:bar', function($one, $two){ echo $one.$two; }); //Prints 'FooBarred'

##Default Paramaters

For having dynamic based urls with default paramaters, you simply wrap your variables in parentheses

//URL: /Foo
$r->get('/:foo(/:bar)', function($foo, $bar=''){ echo $foo.$bar; }); //Prints 'Foo'

//URL: /Foo/Bar
$r->get('/:foo(/:bar)', function($foo, $bar=''){ echo $foo.$bar; }); //Prints 'FooBar'

##Route ordering

Routes should come from most complex and explicit to simplest. notFound should always come last as well.

$r->get('/Foo/Bar', function(){});
$r->get('/:foo/:bar', function(){});
$r->get('/Foo', function(){});
$r->get('/:foo', function(){});
$r->get('/', function(){});
$r->notFound(function(){});