# 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](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:
```
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';
```
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::router();
```
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 t
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。
资源推荐
资源详情
资源评论
收起资源包目录
第九届安徽省GIS大赛项目:安徽科技学院虚拟校园导航系统.zip (706个子文件)
twig.c 30KB
CHANGELOG 32KB
bootstrap.min.css 119KB
default.css 11KB
uploadify.css 2KB
info.css 2KB
phpunit.xml.dist 651B
phpunit.xml.dist 572B
.editorconfig 224B
.gitignore 328B
.gitignore 36B
.gitignore 27B
php_twig.h 1KB
.htaccess 128B
layout.html 10KB
layout2.html 6KB
pano.html 4KB
user_index.html 3KB
pano_hot.html 3KB
index.html 3KB
user_like.html 2KB
pano_share.html 2KB
pano_layout.html 1KB
user_layout.html 706B
user_info.html 579B
pano_image.html 208B
index.html 128B
book.html 122B
base.html 34B
index.html 19B
index.html 17B
index.html 17B
index.html 13B
index.html 11B
index.html 11B
index.html 11B
index.html 5B
pano_book.html 0B
favicon.ico 264KB
pointer.ico 4KB
SchoolVR.iml 778B
bodybg.jpg 29KB
wanhui.jpg 15KB
vr.jpg 7KB
echarts.min.js 312KB
pano2vr_player.js 224KB
raphael.js 216KB
skin.js 87KB
jquery-2.1.4.min.js 82KB
jquery-2.1.4.min.js 82KB
jquery.uploadify.js 64KB
jquery.uploadify.min.js 45KB
bootstrap.min.js 36KB
bootstrap.min.js 36KB
AnkeMapPath.js 29KB
jquery.infinitescroll.js 24KB
jquery.masonry.js 15KB
default.js 13KB
zoom.js 6KB
hot_scatter.js 6KB
prefixfree.min.js 6KB
pano.js 6KB
index.js 5KB
baidu.js 4KB
installed.json 6KB
sftp-config.json 1KB
composer.json 1KB
composer.json 593B
composer.json 578B
composer.json 426B
composer.json 370B
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
composer.lock 7KB
config.m4 221B
README.md 20KB
README.md 5KB
README.md 2KB
README.md 251B
.name 8B
composer.phar 1.51MB
simple_html_dom.php 61KB
Core.php 49KB
Environment.php 36KB
ExpressionParser.php 23KB
TemplateTest.php 22KB
Lexer.php 16KB
Template.php 16KB
Engine.php 13KB
ClassLoader.php 12KB
ExpressionParserTest.php 12KB
Module.php 12KB
Parser.php 11KB
escapingTest.php 11KB
SandboxTest.php 9KB
LexerTest.php 9KB
4da807d27f420c8b708c9f945d43fea7b7341930c8bee9f3575b6d306c06.php 9KB
共 706 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功