Documente Academic
Documente Profesional
Documente Cultură
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
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
/ // 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 } ) ;
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
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
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 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
/ /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 ' ) ;
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 ' ) ;
3/10/2014
i f( F l i g h t : : h a s ( ' i d ' ) ){ / /D os o m e t h i n g }
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 ;? > ' !
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
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 ' ) ) ;
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 >
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
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
} ) ;
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 ( ) ;
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 ' ] ;
http://flightphp.com/learn#methods
10/13
3/10/2014
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 ( ) ;
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
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 } ) ;
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 ) ;
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
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.
http://flightphp.com/learn#methods
13/13