Flight is a fast, simple, and extensible framework for PHP.
It allows you to quickly build RESTful web applications with minimal effort:
Flight is a fast, simple, extensible framework for PHP.
Flight enables you to quickly and easily build RESTful web applications.
require 'flight/Flight.php';
@ -11,21 +11,30 @@ It allows you to quickly build RESTful web applications with minimal effort:
Flight::start();
[Learn more](http://flightphp.com/learn)
## Installation
# Requirements
1\. Download and extract the Flight framework files to your web directory.
Flight requires PHP 5.3 or greater.
# License
Flight is released under the [MIT](http://www.opensource.org/licenses/mit-license.php) license.
# Installation
1\. [Download](https://github.com/mikecao/flight/tarball/master) and extract the Flight framework files to your web directory.
2\. Configure your webserver.
For **Apache**, edit your `.htaccess` file with the following:
For *Apache*, edit your .htaccess file with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
For **Nginx**, add the following to your server declaration:
For *Nginx*, add the following to your server declaration:
server {
location / {
@ -33,7 +42,7 @@ For **Nginx**, add the following to your server declaration:
}
}
3\. Create your `index.php` file.
3\. Create your index.php file.
First include the framework.
@ -49,10 +58,7 @@ Finally, start the framework.
Flight::start();
## Routing
### The Basics
# Routing
Routing in Flight is done by matching a URL pattern with a callback function.
@ -65,6 +71,7 @@ The callback can be any object that is callable. So you can use a regular functi
function hello(){
echo 'hello world!';
}
Flight::route('/', 'hello');
Or a class method:
@ -74,11 +81,12 @@ Or a class method:
echo 'hello world!';
}
}
Flight::route('/', array('Greeting','hello'));
Routes are matched in the order they are defined. The first route to match a request is invoked.
Routes are matched in the order they are defined. The first route to match a request will be invoked.
### Method Routing
## 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.
@ -86,6 +94,7 @@ 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.';
});
@ -98,7 +107,7 @@ You can also map multiple methods to a single callback by using a `|` delimiter:
Method specific routes have precedence over global routes.
### Regular Expressions
## Regular Expressions
You can use regular expressions in your routes:
@ -106,15 +115,9 @@ You can use regular expressions in your routes:
// This will match /user/1234
});
You can also use the wildcard character `*` for matching:
## Named Parameters
Flight::route('/blog/*', function(){
// This will match /blog/2000/02/01
});
### Named Parameters
You can specify named parameters in routes which will be passed along to your callback function.
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)!";
@ -127,15 +130,25 @@ You can also include regular expressions with your named parameters by using the
// But will not match /bob/12345
});
Note that named parameters only match URL segments. If you want to match multiple segments use the `*` wildcard.
## Wildcards
Matching is only done on individual URL segments. If you want to match multiple segments you can use the `*` wildcard.
## Extending
Flight::route('/blog/*', function(){
// This will match /blog/2000/02/01
});
Flight is an extensible framework. Flight's features are individual components that can extended or overridden.
You can map your own methods, register your own classes, or even override existing classes and methods.
To route all requests to a single callback, you can do:
### Mapping Methods
Flight::route('*', function(){
// Do something
});
# Extending
Flight designed to be an extensible framework. The framework comes with a set of default methods and components, but it allows you to map your own methods, register your own classes, or even override existing classes and methods.
## Mapping Methods
To map your own custom method, you use the `map` function:
@ -147,7 +160,7 @@ To map your own custom method, you use the `map` function:
// Call your custom method
Flight::hello('Bob');
### Registering Classes
## Registering Classes
To register your own class, you use the `register` function:
@ -157,10 +170,7 @@ To register your own class, you use the `register` function:
// Get an instance of your class
$user = Flight::user();
If the `User` class is not defined, Flight will look in it's local folder for a file called `User.php` and autoload it.
This is how Flight loads its default classes like Response, Request, and Router.
You can define the constructor parameters for your class by passing in an additional array:
The register method also allows you to pass along parameters to your class constructor. So when you load your custom class, it will come pre-initialized. You can define the constructor parameters by passing in an additional array. Here's an example of loading a database connection:
@ -172,7 +182,7 @@ You can define the constructor parameters for your class by passing in an additi
//
$db = Flight::db();
You can also define a callback that will be executed immediately after class construction.
If you pass in an additional callback parameter, it will be executed immediately after class construction. This allows you to perform any set up procedures for your new object. The callback function takes one parameter, an instance of the new object.
// The callback will be passed the object that was constructed
Flight provides some basic templating functionality by default. To display a view call the `render` method with the
name of the template file and optional template data:
Flight provides some basic templating functionality by default. To display a view template call the `render` method with the name of the template file and optional template data:
The template data you pass in is automatically injected into the template and can be reference like a local variable.
Template files are simply PHP files. If the content of the `hello.php` template file is:
The template data you pass in is automatically injected into the template and can be reference like a local variable. Template files are simply PHP files. If the content of the `hello.php` template file is:
Hello, <?php echo $name; ?>!
Hello, '<?php echo $name; ?>'!
The output would be:
Hello, Bob!
You can manually set view variables at any time by using the `set` method:
You can also manually set view variables by using the set method:
Flight::view()->set('name', 'Bob');
Set variables will be automatically be included when you render a view. So you can simply do:
The variable `name` is now available across all your views. So you can simply do:
Flight::render('hello');
Note that when specifying the name of the template in the `render` method, you can leave out the `.php` extension.
Note that when specifying the name of the template in the render method, you can leave out the .php extension.
By default Flight will look for a `views` directory for template files. You can set an alternate path for your templates
by setting the following config:
By default Flight will look for a `views` directory for template files. You can set an alternate path for your templates by setting the following config:
It is common for websites to have a single layout template file with interchanging content. To render content for a layout you need to pass in a variable name to the `render` method.