Flight

Created: 2014-05-19 15:16
Updated: 2017-06-07 15:52
php

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(){});
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more