Sunteți pe pagina 1din 13

3/10/2014

Flight - An extensible micro-framework for PHP

Flight
An extensible micro-framework for PHP about install learn code

User Guide
Routing Extending Overriding Filtering Variables Views Error Handling Redirects Requests HTTP Caching JSON Configuration Framework Methods

Routing
Routing in Flight is done by matching a URL pattern with a callback function.
F l i g h t : : r o u t e ( ' / ' ,f u n c t i o n ( ) { e c h o' h e l l ow o r l d ! ' ; } ) ;

The callback can be any object that is callable. So you can use a regular function:
f u n c t i o nh e l l o ( ) { e c h o' h e l l ow o r l d ! ' ; } F l i g h t : : r o u t e ( ' / ' ,' h e l l o ' ) ;

Or a class method:
c l a s sG r e e t i n g{ p u b l i cs t a t i cf u n c t i o nh e l l o ( ){ e c h o' h e l l ow o r l d ! ' ; } }
http://flightphp.com/learn#methods 1/13

3/10/2014

Flight - An extensible micro-framework for PHP

F l i g h t : : r o u t e ( ' / ' ,a r r a y ( ' G r e e t i n g ' , ' h e l l o ' ) ) ;

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.
F l i g h t : : r o u t e ( ' G E T/ ' ,f u n c t i o n ( ) { e c h o' Ir e c e i v e daG E Tr e q u e s t . ' ; } ) ; F l i g h t : : r o u t e ( ' P O S T/ ' ,f u n c t i o n ( ) { e c h o' Ir e c e i v e daP O S Tr e q u e s t . ' ; } ) ;

You can also map multiple methods to a single callback by using a |delimiter:
F l i g h t : : r o u t e ( ' G E T | P O S T/ ' ,f u n c t i o n ( ) { e c h o' Ir e c e i v e de i t h e raG E To raP O S Tr e q u e s t . ' ; } ) ;

Regular Expressions
You can use regular expressions in your routes:
F l i g h t : : r o u t e ( ' / u s e r / [ 0 9 ] + ' ,f u n c t i o n ( ) { / /T h i sw i l lm a t c h/ u s e r / 1 2 3 4 } ) ;

Named Parameters
You can specify named parameters in your routes which will be passed along to your callback function.
F l i g h t : : r o u t e ( ' / @ n a m e / @ i d ' ,f u n c t i o n ( $ n a m e ,$ i d ) { e c h o" h e l l o ,$ n a m e( $ i d ) ! " ; } ) ;

You can also include regular expressions with your named parameters by using the :delimiter:
F l i g h t : : r o u t e ( ' / @ n a m e / @ i d : [ 0 9 ] { 3 } ' ,f u n c t i o n ( $ n a m e ,$ i d ) { / /T h i sw i l lm a t c h/ b o b / 1 2 3 / /B u tw i l ln o tm a t c h/ b o b / 1 2 3 4 5 } ) ;

Optional Parameters
You can specify named parameters that are optional for matching by wrapping segments in parentheses.
F l i g h t : : r o u t e ( ' / b l o g ( / @ y e a r ( / @ m o n t h ( / @ d a y ) ) ) ' ,f u n c t i o n ( $ y e a r ,$ m o n t h ,$ d a y ) { / /T h i sw i l lm a t c ht h ef o l l o w i n gU R L S : / // b l o g / 2 0 1 2 / 1 2 / 1 0
http://flightphp.com/learn#methods 2/13

3/10/2014

Flight - An extensible micro-framework for PHP

/ // b l o g / 2 0 1 2 / 1 2 / // b l o g / 2 0 1 2 / // b l o g } ) ;

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.
F l i g h t : : r o u t e ( ' / b l o g / * ' ,f u n c t i o n ( ) { / /T h i sw i l lm a t c h/ b l o g / 2 0 0 0 / 0 2 / 0 1 } ) ;

To route all requests to a single callback, you can do:


F l i g h t : : r o u t e ( ' * ' ,f u n c t i o n ( ) { / /D os o m e t h i n g } ) ;

Passing
You can pass execution on to the next matching route by returning t r u efrom your callback function.
F l i g h t : : r o u t e ( ' / u s e r / @ n a m e ' ,f u n c t i o n ( $ n a m e ) { / /C h e c ks o m ec o n d i t i o n i f( $ n a m e! =" B o b " ){ / /C o n t i n u et on e x tr o u t e r e t u r nt r u e ; } } ) ; F l i g h t : : r o u t e ( ' / u s e r / * ' ,f u n c t i o n ( ) { / /T h i sw i l lg e tc a l l e d } ) ;

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 m a pfunction:
/ /M a py o u rm e t h o d F l i g h t : : m a p ( ' h e l l o ' ,f u n c t i o n ( $ n a m e ) { e c h o" h e l l o$ n a m e ! " ; } ) ; / /C a l ly o u rc u s t o mm e t h o d
http://flightphp.com/learn#methods 3/13

3/10/2014

Flight - An extensible micro-framework for PHP

F l i g h t : : h e l l o ( ' B o b ' ) ;

Registering Classes
To register your own class, you use the r e g i s t e rfunction:
/ /R e g i s t e ry o u rc l a s s F l i g h t : : r e g i s t e r ( ' u s e r ' ,' U s e r ' ) ; / /G e ta ni n s t a n c eo fy o u rc l a s s $ u s e r=F l i g h t : : u s e r ( ) ;

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:
/ /R e g i s t e rc l a s sw i t hc o n s t r u c t o rp a r a m e t e r s F l i g h t : : r e g i s t e r ( ' d b ' ,' P D O ' ,a r r a y ( ' m y s q l : h o s t = l o c a l h o s t ; d n b n a m e = t e s t ' , ' u s e r ' , ' p a s s ' ) ) ; / /G e ta ni n s t a n c eo fy o u rc l a s s / /T h i sw i l lc r e a t ea no b j e c tw i t ht h ed e f i n e dp a r a m e t e r s / / / / n e wP D O ( ' m y s q l : h o s t = l o c a l h o s t ; d n b n a m e = t e s t ' , ' u s e r ' , ' p a s s ' ) ; / / $ d b=F l i g h t : : d b ( ) ;

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.
/ /T h ec a l l b a c kw i l lb ep a s s e dt h eo b j e c tt h a tw a sc o n s t r u c t e d F l i g h t : : r e g i s t e r ( ' d b ' ,' P D O ' ,a r r a y ( ' m y s q l : h o s t = l o c a l h o s t ; d n b n a m e = t e s t ' , ' u s e r ' , ' p a s s ' ) ,f u n c t i o n ( $ d b ) { $ d b > s e t A t t r i b u t e ( P D O : : A T T R _ E R R M O D E ,P D O : : E R R M O D E _ E X C E P T I O N ) ; } ) ;

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 f a l s eas a parameter:
/ /S h a r e di n s t a n c eo ft h ec l a s s $ s h a r e d=F l i g h t : : d b ( ) ; / /N e wi n s t a n c eo ft h ec l a s s $ n e w=F l i g h t : : d b ( f a l s e ) ;

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 n o t F o u n dmethod which sends a
http://flightphp.com/learn#methods 4/13

3/10/2014

Flight - An extensible micro-framework for PHP

generic H T T P4 0 4response. You can override this behavior by using the m a pmethod:
F l i g h t : : m a p ( ' n o t F o u n d ' ,f u n c t i o n ( ) { / /D i s p l a yc u s t o m4 0 4p a g e i n c l u d e' e r r o r s / 4 0 4 . h t m l ' ; } ) ;

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:
/ /R e g i s t e ry o u rc u s t o mc l a s s F l i g h t : : r e g i s t e r ( ' r o u t e r ' ,' M y R o u t e r ' ) ; / /W h e nF l i g h tl o a d st h eR o u t e ri n s t a n c e ,i tw i l ll o a dy o u rc l a s s $ m y r o u t e r=F l i g h t : : r o u t e r ( ) ;

Framework methods like m a pand r e g i s t e rhowever 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. A filter function looks like this:
f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ){ / /F i l t e rc o d e }

Using the passed in variables you can manipulate the input parameters and/or the output. You can have a filter run before a method by doing:
F l i g h t : : b e f o r e ( ' s t a r t ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { / /D os o m e t h i n g } ) ;

You can have a filter run after a method by doing:


F l i g h t : : a f t e r ( ' s t a r t ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { / /D os o m e t h i n g } ) ;

You can add as many filters as you want to any method. They will be called in the order that they are declared. Here's an example of the filtering process:

http://flightphp.com/learn#methods

5/13

3/10/2014

Flight - An extensible micro-framework for PHP

/ /M a pac u s t o mm e t h o d F l i g h t : : m a p ( ' h e l l o ' ,f u n c t i o n ( $ n a m e ) { r e t u r n" H e l l o ,$ n a m e ! " ; } ) ; / /A d dab e f o r ef i l t e r F l i g h t : : b e f o r e ( ' h e l l o ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { / /M a n i p u l a t et h ep a r a m e t e r $ p a r a m s [ 0 ]=' F r e d ' ; } ) ; / /A d da na f t e rf i l t e r F l i g h t : : a f t e r ( ' h e l l o ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { / /M a n i p u l a t et h eo u t p u t $ o u t p u t. ="H a v ean i c ed a y ! " ; } / /I n v o k et h ec u s t o mm e t h o d e c h oF l i g h t : : h e l l o ( ' B o b ' ) ;

This should display:


H e l l oF r e d !H a v ean i c ed a y !

If you have defined multiple filters, you can break the chain by returning f a l s ein any of your filter functions:
F l i g h t : : b e f o r e ( ' s t a r t ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { e c h o' o n e ' ; } ) ; F l i g h t : : b e f o r e ( ' s t a r t ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { e c h o' t w o ' ; / /T h i sw i l le n dt h ec h a i n r e t u r nf a l s e ; } ) ; / /T h i sw i l ln o tg e tc a l l e d F l i g h t : : b e f o r e ( ' s t a r t ' ,f u n c t i o n ( & $ p a r a m s ,& $ o u t p u t ) { e c h o' t h r e e ' ; } ) ;

Note, core methods such as m a pand r e g i s t e rcannot be filtered because they are called directly and not invoked dynamically.

Variables
Flight allows you to save variables so that they can be used anywhere in your application.
/ /S a v ey o u rv a r i a b l e F l i g h t : : s e t ( ' i d ' ,1 2 3 ) ; / /E l s e w h e r ei ny o u ra p p l i c a t i o n $ i d=F l i g h t : : g e t ( ' i d ' ) ;

To see if a variable has been set you can do:


http://flightphp.com/learn#methods 6/13

3/10/2014

Flight - An extensible micro-framework for PHP

i f( F l i g h t : : h a s ( ' i d ' ) ){ / /D os o m e t h i n g }

You can clear a variable by doing:


/ /C l e a r st h ei dv a r i a b l e F l i g h t : : c l e a r ( ' i d ' ) ; / /C l e a r sa l lv a r i a b l e s F l i g h t : : c l e a r ( ) ;

Flight also uses variables for configuration purposes.


F l i g h t : : s e t ( ' f l i g h t . l o g _ e r r o r s ' ,t r u e ) ;

Views
Flight provides some basic templating functionality by default. To display a view template call the r e n d e r method with the name of the template file and optional template data:
F l i g h t : : r e n d e r ( ' h e l l o . p h p ' ,a r r a y ( ' n a m e '= >' B o b ' ) ) ;

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 h e l l o . p h ptemplate file is:
H e l l o ,' < ? p h pe c h o$ n a m e ;? > ' !

The output would be:


H e l l o ,B o b !

You can also manually set view variables by using the set method:
F l i g h t : : v i e w ( ) > s e t ( ' n a m e ' ,' B o b ' ) ;

The variable n a m eis now available across all your views. So you can simply do:
F l i g h t : : r e n d e r ( ' h e l l o ' ) ;

Note that when specifying the name of the template in the render method, you can leave out the . p h p extension. By default Flight will look for a v i e w sdirectory for template files. You can set an alternate path for your templates by setting the following config:

http://flightphp.com/learn#methods

7/13

3/10/2014

Flight - An extensible micro-framework for PHP

F l i g h t : : s e t ( ' f l i g h t . v i e w s . p a t h ' ,' / p a t h / t o / v i e w s ' ) ;

Layouts
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 r e n d e rmethod.
F l i g h t : : r e n d e r ( ' h e a d e r ' ,a r r a y ( ' h e a d i n g '= >' H e l l o ' ) ,' h e a d e r _ c o n t e n t ' ) ; F l i g h t : : r e n d e r ( ' b o d y ' ,a r r a y ( ' b o d y '= >' W o r l d ' ) ,' b o d y _ c o n t e n t ' ) ;

Your view will then have saved variables called h e a d e r _ c o n t e n tand b o d y _ c o n t e n t . You can then render your layout by doing:
F l i g h t : : r e n d e r ( ' l a y o u t ' ,a r r a y ( ' t i t l e '= >' H o m eP a g e ' ) ) ;

If the template files looks like this: h e a d e r . p h p :


< h 1 > < ? p h pe c h o$ h e a d i n g ;? > < / h 1 >

b o d y . p h p :
< d i v > < ? p h pe c h o$ b o d y ;? > < / d i v >

l a y o u t . p h p :
< h t m l > < h e a d > < t i t l e > < ? p h pe c h o$ t i t l e ;? > < / t i t l e > < / h e a d > < b o d y > < ? p h pe c h o$ h e a d e r _ c o n t e n t ;? > < ? p h pe c h o$ b o d y _ c o n t e n t ;? > < / b o d y > < / h t m l >

The output would be:


< h t m l > < h e a d > < t i t l e > H o m eP a g e < / t i t l e > < / h e a d > < b o d y > < h 1 > H e l l o < / h 1 > < d i v > W o r l d < / d i v > < / b o d y > < / h t m l >

Custom Views
Flight allows you to swap out the default view engine simply by registering your own view class. Here's
http://flightphp.com/learn#methods 8/13

3/10/2014

Flight - An extensible micro-framework for PHP

how you would use the Smarty template engine for your views:
/ /L o a dS m a r t yl i b r a r y r e q u i r e' . / S m a r t y / l i b s / S m a r t y . c l a s s . p h p ' ; / /R e g i s t e rS m a r t ya st h ev i e wc l a s s / /A l s op a s sac a l l b a c kf u n c t i o nt oc o n f i g u r eS m a r t yo nl o a d F l i g h t : : r e g i s t e r ( ' v i e w ' ,' S m a r t y ' ,a r r a y ( ) ,f u n c t i o n ( $ s m a r t y ) { $ s m a r t y > t e m p l a t e _ d i r=' . / t e m p l a t e s / ' ; $ s m a r t y > c o m p i l e _ d i r=' . / t e m p l a t e s _ c / ' ; $ s m a r t y > c o n f i g _ d i r=' . / c o n f i g / ' ; $ s m a r t y > c a c h e _ d i r=' . / c a c h e / ' ; } ) ; / /A s s i g nt e m p l a t ed a t a F l i g h t : : v i e w ( ) > a s s i g n ( ' n a m e ' ,' B o b ' ) ; / /D i s p l a yt h et e m p l a t e F l i g h t : : v i e w ( ) > d i s p l a y ( ' h e l l o . t p l ' ) ;

For completeness, you should also override Flight's default render method:
F l i g h t : : m a p ( ' r e n d e r ' ,f u n c t i o n ( $ t e m p l a t e ,$ d a t a ) { F l i g h t : : v i e w ( ) > a s s i g n ( $ d a t a ) ; F l i g h t : : v i e w ( ) > d i s p l a y ( $ t e m p l a t e ) ; } ) ;

Error Handling
Errors and Exceptions
All errors and exceptions are caught by Flight and passed to the e r r o rmethod. The default behavior is to send a generic H T T P5 0 0I n t e r n a lS e r v e rE r r o rresponse with some error information. You can override this behavior for your own needs:
F l i g h t : : m a p ( ' e r r o r ' ,f u n c t i o n ( E x c e p t i o n$ e x ) { / /H a n d l ee r r o r e c h o$ e x > g e t T r a c e A s S t r i n g ( ) ; } ) ;

By default errors are not logged to the web server. You can enable this by changing the config:
F l i g h t : : s e t ( ' f l i g h t . l o g _ e r r o r s ' ,t r u e ) ;

Not Found
When a URL can't be found, Flight calls the n o t F o u n dmethod. The default behavior is to send an H T T P 4 0 4N o tF o u n dresponse with a simple message. You can override this behavior for your own needs:
F l i g h t : : m a p ( ' n o t F o u n d ' ,f u n c t i o n ( ) { / /H a n d l en o tf o u n d
http://flightphp.com/learn#methods 9/13

3/10/2014

Flight - An extensible micro-framework for PHP

} ) ;

Redirects
You can redirect the current request by using the r e d i r e c tmethod and passing in a new URL:
F l i g h t : : r e d i r e c t ( ' / n e w / l o c a t i o n ' ) ;

By default Flight sends a HTTP 303 status code. You can optionally set a custom code:
F l i g h t : : r e d i r e c t ( ' / n e w / l o c a t i o n ' ,4 0 1 ) ;

Requests
Flight encapsulates the HTTP request into a single object, which can be accessed by doing:
$ r e q u e s t=F l i g h t : : r e q u e s t ( ) ;

The request object provides the following properties:


u r l-T h eU R Lb e i n gr e q u e s t e d b a s e-T h ep a r e n ts u b d i r e c t o r yo ft h eU R L m e t h o d-T h er e q u e s tm e t h o d( G E T ,P O S T ,P U T ,D E L E T E ) r e f e r r e r-T h er e f e r r e rU R L i p-I Pa d d r e s so ft h ec l i e n t a j a x-W h e t h e rt h er e q u e s ti sa nA J A Xr e q u e s t s c h e m e-T h es e r v e rp r o t o c o l( h t t p ,h t t p s ) u s e r _ a g e n t-B r o w s e ri n f o r m a t i o n b o d y-R a wd a t af r o mt h er e q u e s tb o d y t y p e-T h ec o n t e n tt y p e l e n g t h-T h ec o n t e n tl e n g t h q u e r y-Q u e r ys t r i n gp a r a m e t e r s d a t a-P o s tp a r a m e t e r s c o o k i e s-C o o k i ep a r a m e t e r s f i l e s-U p l o a d e df i l e s s e c u r e-W h e t h e rt h ec o n n e c t i o ni ss e c u r e a c c e p t-H T T Pa c c e p tp a r a m e t e r s p r o x y _ i p-P r o x yI Pa d d r e s so ft h ec l i e n t

You can access the q u e r y ,d a t a ,c o o k i e s , and f i l e sproperties as arrays or objects. So, to get a query string parameter, you can do:
$ i d=F l i g h t : : r e q u e s t ( ) > q u e r y [ ' i d ' ] ;

Or you can do:


$ i d=F l i g h t : : r e q u e s t ( ) > q u e r y > i d ;

http://flightphp.com/learn#methods

10/13

3/10/2014

Flight - An extensible micro-framework for PHP

HTTP Caching
Flight provides built-in support for HTTP level caching. If the caching condition is met, Flight will return an HTTP 3 0 4N o tM o d i f i e dresponse. 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 l a s t M o d i f i e dmethod 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.
F l i g h t : : r o u t e ( ' / n e w s ' ,f u n c t i o n ( ) { F l i g h t : : l a s t M o d i f i e d ( 1 2 3 4 5 6 7 8 9 0 ) ; e c h o' T h i sc o n t e n tw i l lb ec a c h e d . ' ; } ) ;

ETag
E T a gcaching is similar to L a s t M o d i f i e d , except you can specify any id you want for the resource:
F l i g h t : : r o u t e ( ' / n e w s ' ,f u n c t i o n ( ) { F l i g h t : : e t a g ( ' m y u n i q u e i d ' ) ; e c h o' T h i sc o n t e n tw i l lb ec a c h e d . ' ; } ) ;

Keep in mind that calling either l a s t M o d i f i e dor e t a gwill both set and check the cache value. If the cache value is the same between requests, Flight will immediately send an H T T P3 0 4response and stop processing.

Stopping
You can stop the framework at any point by calling the h a l tmethod:
F l i g h t : : h a l t ( ) ;

You can also specify an optional H T T Pstatus code and message:


F l i g h t : : h a l t ( 2 0 0 ,' B er i g h tb a c k . . . ' ) ;

Calling h a l twill discard any response content up to that point. If you want to stop the framework and output the current response, use the s t o pmethod:
F l i g h t : : s t o p ( ) ;

JSON
Flight provides support for sending JSON and JSONP responses. To send a JSON response you pass some
http://flightphp.com/learn#methods 11/13

3/10/2014

Flight - An extensible micro-framework for PHP

data to be JSON encoded:


F l i g h t : : j s o n ( a r r a y ( ' i d '= >1 2 3 ) ) ;

For JSONP requests you would use the j s o n pmethod. You can optionally pass in the query parameter name you are using to define your callback function:
F l i g h t : : j s o n p ( a r r a y ( ' i d '= >1 2 3 ) ,' q ' ) ;

So, when making a GET request using ? q = m y _ f u n c , you should receive the output:
m y _ f u n c ( { " i d " : 1 2 3 } ) ;

If you don't pass in a query parameter name it will default to j s o n p .

Configuration
You can customize certain behaviors of Flight by setting configuration values through the s e tmethod.
F l i g h t : : s e t ( ' f l i g h t . l o g _ e r r o r s ' ,t r u e ) ;

The following is a list of all the available configuration settings.


f l i g h t . b a s e _ u r l-O v e r r i d et h eb a s eu r lo ft h er e q u e s t .( d e f a u l t :n u l l ) f l i g h t . h a n d l e _ e r r o r s-A l l o wF l i g h tt oh a n d l ea l le r r o r si n t e r n a l l y .( d e f a u l t :t r u e ) f l i g h t . l o g _ e r r o r s-L o ge r r o r st ot h ew e bs e r v e r ' se r r o rl o gf i l e .( d e f a u l t :f a l s e ) f l i g h t . v i e w s . p a t h-D i r e c t o r yc o n t a i n i n gv i e wt e m p l a t ef i l e s( d e f a u l t :. / v i e w s )

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 can be filtered or overridden.

Core Methods
F l i g h t : : m a p ( $ n a m e ,$ c a l l b a c k )/ /C r e a t e sac u s t o mf r a m e w o r km e t h o d . F l i g h t : : r e g i s t e r ( $ n a m e ,$ c l a s s ,[ $ p a r a m s ] ,[ $ c a l l b a c k ] )/ /R e g i s t e r sac l a s st oaf r a m e w o r km e t h o d . F l i g h t : : b e f o r e ( $ n a m e ,$ c a l l b a c k )/ /A d d saf i l t e rb e f o r eaf r a m e w o r km e t h o d . F l i g h t : : a f t e r ( $ n a m e ,$ c a l l b a c k )/ /A d d saf i l t e ra f t e raf r a m e w o r km e t h o d . F l i g h t : : p a t h ( $ p a t h )/ /A d d sap a t hf o ra u t o l o a d i n gc l a s s e s . F l i g h t : : g e t ( $ k e y )/ /G e t sav a r i a b l e . F l i g h t : : s e t ( $ k e y ,$ v a l u e )/ /S e t sav a r i a b l e . F l i g h t : : h a s ( $ k e y )/ /C h e c k si fav a r i a b l ei ss e t . F l i g h t : : c l e a r ( [ $ k e y ] )/ /C l e a r sav a r i a b l e .

Extensible Methods
http://flightphp.com/learn#methods 12/13

3/10/2014

Flight - An extensible micro-framework for PHP

F l i g h t : : s t a r t ( )/ /S t a r t st h ef r a m e w o r k . F l i g h t : : s t o p ( )/ /S t o p st h ef r a m e w o r ka n ds e n d sar e s p o n s e . F l i g h t : : h a l t ( [ $ c o d e ] ,[ $ m e s s a g e ] )/ /S t o pt h ef r a m e w o r kw i t ha no p t i o n a ls t a t u sc o d ea n dm e s s a g e . F l i g h t : : r o u t e ( $ p a t t e r n ,$ c a l l b a c k )/ /M a p saU R Lp a t t e r nt oac a l l b a c k . F l i g h t : : r e d i r e c t ( $ u r l ,[ $ c o d e ] )/ /R e d i r e c t st oa n o t h e rU R L . F l i g h t : : r e n d e r ( $ f i l e ,[ $ d a t a ] ,[ $ k e y ] )/ /R e n d e r sat e m p l a t ef i l e . F l i g h t : : e r r o r ( $ e x c e p t i o n )/ /S e n d sa nH T T P5 0 0r e s p o n s e . F l i g h t : : n o t F o u n d ( )/ /S e n d sa nH T T P4 0 4r e s p o n s e . F l i g h t : : e t a g ( $ i d ,[ $ t y p e ] )/ /P e r f o r m sE T a gH T T Pc a c h i n g . F l i g h t : : l a s t M o d i f i e d ( $ t i m e )/ /P e r f o r m sl a s tm o d i f i e dH T T Pc a c h i n g . F l i g h t : : j s o n ( $ d a t a ,[ $ c o d e ] ,[ $ e n c o d e ] )/ /S e n d saJ S O Nr e s p o n s e . F l i g h t : : j s o n p ( $ d a t a ,[ $ p a r a m ] ,[ $ c o d e ] ,[ $ e n c o d e ] )/ /S e n d saJ S O N Pr e s p o n s e .

Any custom methods added with m a pand r e g i s t e rcan also be filtered.

Framework Instance
Instead of running Flight as a global static class, you can optionally run it as an object instance.
r e q u i r e' f l i g h t / a u t o l o a d . p h p ' ; u s ef l i g h t \ E n g i n e ; $ a p p=n e wE n g i n e ( ) ; $ a p p > r o u t e ( ' / ' ,f u n c t i o n ( ) { e c h o' h e l l ow o r l d ! ' ; } ) ; $ a p p > s t a r t ( ) ;

All of the existing static methods are available as regular class methods.

Copyright 2013 Mike Cao Powered by Flight

http://flightphp.com/learn#methods

13/13

S-ar putea să vă placă și