Flight is 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.
Flight is 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.
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:
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:
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.
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.
```php
// The callback will be passed the object that was constructed
@ -289,14 +306,17 @@ $shared = Flight::db();
$new = Flight::db(false);
```
Keep in mind that mapped methods have precedence over registered classes. If you declare both using the same name, only the mapped method will be invoked.
Keep in mind that mapped methods have precedence over registered classes. If you
declare both using the same name, only the mapped method will be invoked.
# Overriding
Flight allows you to override its default functionality to suit your own needs, without having to modify any code.
Flight allows you to override its default functionality to suit your own needs,
without having to modify any code.
For example, when Flight cannot match a URL to a route, it invokes the `notFound` method which sends a generic `HTTP 404` response.
You can override this behavior by using the `map` method:
For example, when Flight cannot match a URL to a route, it invokes the `notFound`
method which sends a generic `HTTP 404` response. You can override this behavior
Framework methods like `map` and `register` however cannot be overridden. You will get an error if you try to do so.
Framework methods like `map` and `register` however cannot be overridden. You will
get an error if you try to do so.
# Filtering
Flight allows you to filter methods before and after they are called. There are no predefined hooks you need to memorize. You can filter any of the default framework methods as well as any custom methods that you've mapped.
Flight allows you to filter methods before and after they are called. There are no
predefined hooks you need to memorize. You can filter any of the default framework
methods as well as any custom methods that you've mapped.
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:
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
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:
```php
Hello, '<?php echo $name; ?>'!
@ -465,9 +495,11 @@ 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 to be used in a layout, you can pass in an optional parameter to the `render` method.
It is common for websites to have a single layout template file with interchanging
content. To render content to be used in a layout, you can pass in an optional
Flight allows you to swap out the default view engine simply by registering your own view class. Here's how you would use the [Smarty](http://www.smarty.net/) template engine for your views:
Flight allows you to swap out the default view engine simply by registering your
own view class. Here's how you would use the [Smarty](http://www.smarty.net/)
Flight provides built-in support for HTTP level caching. If the caching condition is met, Flight will return an HTTP `304 Not Modified` response. The next time the client requests the same resource, they will be prompted to use their locally cached version.
Flight provides built-in support for HTTP level caching. If the caching condition
is met, Flight will return an HTTP `304 Not Modified` response. The next time the
client requests the same resource, they will be prompted to use their locally
cached version.
## Last-Modified
You can use the `lastModified` method and pass in a UNIX timestamp to set the date and time a page was last modified. The client will continue to use their cache until the last modified value is changed.
You can use the `lastModified` method and pass in a UNIX timestamp to set the date
and time a page was last modified. The client will continue to use their cache until
Keep in mind that calling either `lastModified` or `etag` will both set and check the cache value. If the cache value is the same between requests, Flight will immediately send an `HTTP 304` response and stop processing.
Keep in mind that calling either `lastModified` or `etag` will both set and check the
cache value. If the cache value is the same between requests, Flight will immediately
send an `HTTP 304` response and stop processing.
# Stopping
@ -696,7 +748,8 @@ You can also specify an optional `HTTP` status code and message:
Flight::halt(200, 'Be right back...');
```
Calling `halt` will discard any response content up to that point. If you want to stop the framework and output the current response, use the `stop` method:
Calling `halt` will discard any response content up to that point. If you want to stop
the framework and output the current response, use the `stop` method:
```php
Flight::stop();
@ -704,13 +757,15 @@ Flight::stop();
# JSON
Flight provides support for sending JSON and JSONP responses. To send a JSON response you pass some data to be JSON encoded:
Flight provides support for sending JSON and JSONP responses. To send a JSON response you
pass some data to be JSON encoded:
```php
Flight::json(array('id' => 123));
```
For JSONP requests you, can optionally pass in the query parameter name you are using to define your callback function:
For JSONP requests you, can optionally pass in the query parameter name you are
using to define your callback function:
```php
Flight::jsonp(array('id' => 123), 'q');
@ -727,7 +782,8 @@ If you don't pass in a query parameter name it will default to `jsonp`.
# Configuration
You can customize certain behaviors of Flight by setting configuration values through the `set` method.
You can customize certain behaviors of Flight by setting configuration values
through the `set` method.
```php
Flight::set('flight.log_errors', true);
@ -742,7 +798,10 @@ The following is a list of all the available configuration settings:
# Framework Methods
Flight is designed to be easy to use and understand. The following is the complete set of methods for the framework. It consists of core methods, which are regular static methods, and extensible methods, which are mapped methods that can be filtered or overridden.
Flight is designed to be easy to use and understand. The following is the complete
set of methods for the framework. It consists of core methods, which are regular
static methods, and extensible methods, which are mapped methods that can be filtered
or overridden.
## Core Methods
@ -781,7 +840,8 @@ Any custom methods added with `map` and `register` can also be filtered.
# Framework Instance
Instead of running Flight as a global static class, you can optionally run it as an object instance.
Instead of running Flight as a global static class, you can optionally run it
as an object instance.
```php
require 'flight/autoload.php';
@ -797,4 +857,5 @@ $app->route('/', function(){
$app->start();
```
So instead of calling the static method, you would call the instance method with the same name on the Engine object.
So instead of calling the static method, you would call the instance method with