Download the PHP package masroore/flight without Composer
On this page you can find all versions of the php package masroore/flight. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download masroore/flight
More information about masroore/flight
Files in masroore/flight
Package flight
Short Description Flight is a fast, simple, extensible framework for PHP. Flight enables you to quickly and easily build RESTful web applications.
License MIT
Homepage http://flightphp.com
Informations about the package flight
What is Flight?
Flight is a fast, simple, extensible framework for PHP. Flight enables you to quickly and easily build RESTful web applications.
Requirements
Flight requires PHP 7.4
or greater.
License
Flight is released under the MIT license.
Installation
1. Download the files.
If you're using Composer, you can run the following command:
OR you can download them directly and extract them to your web directory.
2. Configure your webserver.
For Apache, edit your .htaccess
file with the following:
Note: If you need to use flight in a subdirectory add the line RewriteBase /subdir/
just after RewriteEngine On
.
For Nginx, add the following to your server declaration:
3. Create your index.php
file.
First include the framework.
If you're using Composer, run the autoloader instead.
Then define a route and assign a function to handle the request.
Finally, start the framework.
Routing
Routing in Flight is done by matching a URL pattern with a callback function.
The callback can be any object that is callable. So you can use a regular function:
Or a class method:
Or an object method:
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.
You can also map multiple methods to a single callback by using a |
delimiter:
Regular Expressions
You can use regular expressions in your routes:
Named Parameters
You can specify named parameters in your routes which will be passed along to your callback function.
You can also include regular expressions with your named parameters by using
the :
delimiter:
Matching regex groups ()
with named parameters isn't supported.
Optional Parameters
You can specify named parameters that are optional for matching by wrapping segments in parentheses.
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.
To route all requests to a single callback, you can do:
Passing
You can pass execution on to the next matching route by returning true
from
your callback function.
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.
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:
Registering Classes
To register your own class, you use the register
function:
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.
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:
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:
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:
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.
A filter function looks like this:
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:
You can have a filter run after a method by doing:
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:
This should display:
Hello Fred! Have a nice day!
If you have defined multiple filters, you can break the chain by returning false
in any of your filter functions:
Note, core methods such as map
and register
cannot 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.
To see if a variable has been set you can do:
You can clear a variable by doing:
Flight also uses variables for configuration purposes.
Views
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:
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 output would be:
Hello, Bob!
You can also manually set view variables by using the set method:
The variable name
is now available across all your views. So you can simply do:
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:
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 render
method.
Your view will then have saved variables called header_content
and body_content
.
You can then render your layout by doing:
If the template files looks like this:
header.php
:
body.php
:
layout.php
:
The output would be:
Custom 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 template engine for your views:
For completeness, you should also override Flight's default render method:
Error Handling
Errors and Exceptions
All errors and exceptions are caught by Flight and passed to the error
method.
The default behavior is to send a generic HTTP 500 Internal Server Error
response with some error information.
You can override this behavior for your own needs:
By default errors are not logged to the web server. You can enable this by changing the config:
Not Found
When a URL can't be found, Flight calls the notFound
method. The default
behavior is to send an HTTP 404 Not Found
response with a simple message.
You can override this behavior for your own needs:
Redirects
You can redirect the current request by using the redirect
method and passing
in a new URL:
By default Flight sends a HTTP 303 status code. You can optionally set a custom code:
Requests
Flight encapsulates the HTTP request into a single object, which can be accessed by doing:
The request object provides the following properties:
You can access the query
, data
, cookies
, and files
properties
as arrays or objects.
So, to get a query string parameter, you can do:
Or you can do:
RAW Request Body
To get the raw HTTP request body, for example when dealing with PUT requests, you can do:
JSON Input
If you send a request with the type application/json
and the data {"id": 123}
it will be available
from the data
property:
HTTP Caching
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.
ETag
ETag
caching is similar to Last-Modified
, except you can specify any id you
want for the resource:
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
You can stop the framework at any point by calling the halt
method:
You can also specify an optional HTTP
status code and message:
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:
JSON
Flight provides support for sending JSON and JSONP responses. To send a JSON response you pass some data to be JSON encoded:
For JSONP requests you, can optionally pass in the query parameter name you are using to define your callback function:
So, when making a GET request using ?q=my_func
, you should receive the output:
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.
The following is a list of all the available configuration settings:
flight.base_url - Override the base url of the request. (default: null)
flight.case_sensitive - Case sensitive matching for URLs. (default: false)
flight.handle_errors - Allow Flight to handle all errors internally. (default: true)
flight.log_errors - Log errors to the web server's error log file. (default: false)
flight.views.path - Directory containing view template files. (default: ./views)
flight.views.extension - View template file extension. (default: .php)
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.
Core Methods
Extensible Methods
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.
So instead of calling the static method, you would call the instance method with the same name on the Engine object.