# What is Flight?
Flight is a fast, simple, extensible framework for PHP. Flight enables you to
quickly and easily build RESTful web applications.
```php
require 'flight/Flight.php';
Flight::route('/', function(){
echo 'hello world!';
});
Flight::start();
```
[Learn more](http://flightphp.com/learn)
# Requirements
Flight requires `PHP 5.3` or greater.
# License
Flight is released under the [MIT](http://flightphp.com/license) license.
# Installation
1\. Download the files.
If you're using [Composer](https://getcomposer.org/), you can run the following command:
```
composer require mikecao/flight
```
OR you can [download](https://github.com/mikecao/flight/archive/master.zip) them directly
and extract them to your web directory.
2\. Configure your webserver.
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:
```
server {
location / {
try_files $uri $uri/ /index.php;
}
}
```
3\. Create your `index.php` file.
First include the framework.
```php
require 'flight/Flight.php';
```
If you're using Composer, run the autoloader instead.
```php
require 'vendor/autoload.php';
```
Then define a route and assign a function to handle the request.
```php
Flight::route('/', function(){
echo 'hello world!';
});
```
Finally, start the framework.
```php
Flight::start();
```
# Routing
Routing in Flight is done by matching a URL pattern with a callback function.
```php
Flight::route('/', function(){
echo 'hello world!';
});
```
The callback can be any object that is callable. So you can use a regular function:
```php
function hello(){
echo 'hello world!';
}
Flight::route('/', 'hello');
```
Or a class method:
```php
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.
```php
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:
```php
Flight::route('GET|POST /', function(){
echo 'I received either a GET or a POST request.';
});
```
## Regular Expressions
You can use regular expressions in your routes:
```php
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.
```php
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:
```php
Flight::route('/@name/@id:[0-9]{3}', function($name, $id){
// This will match /bob/123
// But will not match /bob/12345
});
```
## Optional Parameters
You can specify named parameters that are optional for matching by wrapping
segments in parentheses.
```php
Flight::route('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
// This will match the following URLS:
// /blog/2012/12/10
// /blog/2012/12
// /blog/2012
// /blog
});
```
Any optional parameters that are not matched will be passed in as NULL.
## Wildcards
Matching is only done on individual URL segments. If you want to match multiple
segments you can use the `*` wildcard.
```php
Flight::route('/blog/*', function(){
// This will match /blog/2000/02/01
});
```
To route all requests to a single callback, you can do:
```php
Flight::route('*', function(){
// Do something
});
```
## Passing
You can pass execution on to the next matching route by returning `true` from
your callback function.
```php
Flight::route('/user/@name', function($name){
// Check some condition
if ($name != "Bob") {
// Continue to next route
return true;
}
});
Flight::route('/user/*', function(){
// This will get called
});
```
## Route Info
If you want to inspect the matching route information, you can request for the route
object to be passed to your callback by passing in `true` as the third parameter in
the route method. The route object will always be the last parameter passed to your
callback function.
```php
Flight::route('/', function($route){
// Array of HTTP methods matched against
$route->methods;
// Array of named parameters
$route->params;
// Matching regular expression
$route->regex;
// Contains the contents of any '*' used in the URL pattern
$route->splat;
}, true);
```
# Extending
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.
## Mapping Methods
To map your own custom method, you use the `map` function:
```php
// Map your method
Flight::map('hello', function($name){
echo "hello $name!";
});
// Call your custom method
Flight::hello('Bob');
```
## Registering Classes
To register your own class, you use the `register` function:
```php
// Register your class
Flight::register('user', 'User');
// Get an instance of your class
$user = Flight::user();
```
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:
```php
// Register class with constructor parameters
Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'));
// Get an instance of your class
// This will create an object with the defined parameters
//
// new PDO('mysql:host=localhost;dbname=test','user','pass');
//
$db = Flight::db();
```
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
Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'), function($db){
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
});
```
By default, every time you load your class you will get a shared instance.
To get a new instance of a class, simply pass in `false` as a parameter:
```php
// Shared instance of the class
$shared = Flight::db();
// New instance of the class
$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.
# Overriding
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:
```php
Flight::map('notFound', function(){
// Display custom 404 page
include 'errors/404.html';
});
```
Flight also allows you to replace core components of the framework.
For example you can replace the default Router class with your own custom class:
```php
// Register your custom class
Flight::register('router', 'MyRouter');
// When Flight loads the Router instance, it will load your class
$myrouter = Flight
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计修改为进销存系统(o__o 宝宝想哭的心都有了).zip (246个子文件)
xunsearch-full-latest.tar.bz2 9.87MB
bootstrap.css 121KB
bootstrap.min.css 118KB
bootstrap-ie6.css 110KB
font-awesome.min.css 21KB
fakeLoader.css 17KB
dataTables.bootstrap.css 7KB
highlight.css 6KB
sb-admin-2.css 5KB
reset.css 5KB
structure.css 4KB
guide.css 3KB
api.css 2KB
style.css 2KB
ie.css 1KB
metisMenu.min.css 781B
style.css 416B
pace-theme-minimal.css 411B
fontawesome-webfont.eot 55KB
glyphicons-halflings-regular.eot 20KB
info.gif 698B
note.gif 436B
tip.gif 341B
.gitignore 6B
xs_php_manual.hhc 15KB
manual.hhc 6KB
xs_php_manual.hhk 61KB
manual.hhk 49KB
xs_php_manual.hhp 654B
manual.hhp 627B
XSSearch.html 205KB
XSIndex.html 115KB
XSServer.html 80KB
XSFieldMeta.html 71KB
XS.html 64KB
XSDocument.html 58KB
XSTokenizerScws.html 49KB
XSUtil.html 46KB
XSFieldScheme.html 41KB
XSDatabaseDataSource.html 31KB
search.query.html 26KB
ini.first.html 23KB
XSJsonDataSource.html 22KB
XSDataSource.html 20KB
XSCommand.html 19KB
search.search.html 18KB
XSDatabaseMySQL.html 18KB
XSDatabaseMySQLI.html 17KB
util.indexer.html 16KB
XSException.html 16KB
XSCsvDataSource.html 16KB
XSDatabasePDO.html 14KB
XSDatabasePgSQL.html 14KB
XSComponent.html 14KB
class.document.html 13KB
special.synonym.html 13KB
special.composer.html 12KB
search.overview.html 12KB
XSDatabaseSQLite3.html 12KB
XSErrorException.html 11KB
XSDatabaseSQLite.html 11KB
XSDatabase.html 11KB
search.fix.html 10KB
XSDatabasePDO_MySQL.html 9KB
ini.tokenizer.html 9KB
XSTokenizerXstep.html 9KB
XSTokenizerXlen.html 9KB
start.installation.html 9KB
ini.guide.html 9KB
XSDatabasePDO_PgSQL.html 8KB
XSTokenizerSplit.html 8KB
start.changelog.html 8KB
special.scws.html 7KB
class.exception.html 7KB
search.facet.html 7KB
XSDatabasePDO_SQLite.html 6KB
search.count.html 6KB
index.overview.html 6KB
util.logger.html 6KB
XSDataFilter.html 6KB
XSDebugFilter.html 6KB
index.html 6KB
index.del.html 6KB
start.demo.html 5KB
ABOUT.html 5KB
class.xs.html 5KB
XSTokenizer.html 5KB
index.add.html 5KB
util.quest.html 5KB
index.update.html 5KB
DOWNLOAD.html 5KB
XSTokenizerFull.html 4KB
XSTokenizerNone.html 4KB
index.rebuild.html 4KB
class.overview.html 4KB
toc.html 4KB
start.about.html 4KB
util.skel.html 4KB
ARCHITECTURE.html 4KB
SUPPORT.html 4KB
共 246 条
- 1
- 2
- 3
资源评论
马coder
- 粉丝: 1206
- 资源: 6602
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功