diff --git a/README.md b/README.md index e078d00..469c5f7 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ ![](https://user-images.githubusercontent.com/104888/50957476-9c4acb80-14be-11e9-88ce-6447364dc1bb.png) ![](https://img.shields.io/badge/PHPStan-level%206-brightgreen.svg?style=flat) -Flight is a fast, simple, extensible framework for PHP. Flight enables you to +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::route('/', function() { + echo 'hello world!'; }); Flight::start(); @@ -30,14 +30,15 @@ Flight is released under the [MIT](http://flightphp.com/license) license. 1\. Download the files. -If you're using [Composer](https://getcomposer.org/), you can run the following command: +If you're using [Composer](https://getcomposer.org/), you can run the following +command: -``` +```bash 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. +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. @@ -50,15 +51,16 @@ RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] ``` -**Note**: If you need to use flight in a subdirectory add the line `RewriteBase /subdir/` just after `RewriteEngine On`. +**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: ``` server { - location / { - try_files $uri $uri/ /index.php; - } + location / { + try_files $uri $uri/ /index.php; + } } ``` 3\. Create your `index.php` file. @@ -78,8 +80,8 @@ require 'vendor/autoload.php'; Then define a route and assign a function to handle the request. ```php -Flight::route('/', function(){ - echo 'hello world!'; +Flight::route('/', function () { + echo 'hello world!'; }); ``` @@ -94,16 +96,16 @@ Flight::start(); Routing in Flight is done by matching a URL pattern with a callback function. ```php -Flight::route('/', function(){ - echo 'hello world!'; +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!'; +function hello() { + echo 'hello world!'; } Flight::route('/', 'hello'); @@ -113,9 +115,9 @@ Or a class method: ```php class Greeting { - public static function hello() { - echo 'hello world!'; - } + public static function hello() { + echo 'hello world!'; + } } Flight::route('/', array('Greeting', 'hello')); @@ -126,18 +128,18 @@ Or an object method: ```php class Greeting { - public function __construct() { - $this->name = 'John Doe'; - } + public function __construct() { + $this->name = 'John Doe'; + } - public function hello() { - echo "Hello, {$this->name}!"; - } + public function hello() { + echo "Hello, {$this->name}!"; + } } $greeting = new Greeting(); -Flight::route('/', array($greeting, 'hello')); +Flight::route('/', array($greeting, 'hello')); ``` Routes are matched in the order they are defined. The first route to match a @@ -149,20 +151,20 @@ By default, route patterns are matched against all request methods. You can resp to specific methods by placing an identifier before the URL. ```php -Flight::route('GET /', function(){ - echo 'I received a GET request.'; +Flight::route('GET /', function () { + echo 'I received a GET request.'; }); -Flight::route('POST /', function(){ - echo 'I received a POST 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.'; +Flight::route('GET|POST /', function () { + echo 'I received either a GET or a POST request.'; }); ``` @@ -171,8 +173,8 @@ Flight::route('GET|POST /', function(){ You can use regular expressions in your routes: ```php -Flight::route('/user/[0-9]+', function(){ - // This will match /user/1234 +Flight::route('/user/[0-9]+', function () { + // This will match /user/1234 }); ``` @@ -182,8 +184,8 @@ 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)!"; +Flight::route('/@name/@id', function(string $name, string $id) { + echo "hello, $name ($id)!"; }); ``` @@ -191,9 +193,9 @@ 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 +Flight::route('/@name/@id:[0-9]{3}', function(string $name, string $id) { + // This will match /bob/123 + // But will not match /bob/12345 }); ``` @@ -205,13 +207,16 @@ 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){ +Flight::route( + '/blog(/@year(/@month(/@day)))', + function(?string $year, ?string $month, ?string $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. @@ -222,16 +227,16 @@ 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 +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 +Flight::route('*', function () { + // Do something }); ``` @@ -241,16 +246,16 @@ 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/@name', function (string $name) { + // Check some condition + if ($name != "Bob") { + // Continue to next route + return true; + } }); -Flight::route('/user/*', function(){ - // This will get called +Flight::route('/user/*', function () { + // This will get called }); ``` @@ -262,18 +267,18 @@ the route method. The route object will always be the last parameter passed to y callback function. ```php -Flight::route('/', function($route){ - // Array of HTTP methods matched against - $route->methods; +Flight::route('/', function(\flight\net\Route $route) { + // Array of HTTP methods matched against + $route->methods; - // Array of named parameters - $route->params; + // Array of named parameters + $route->params; - // Matching regular expression - $route->regex; + // Matching regular expression + $route->regex; - // Contains the contents of any '*' used in the URL pattern - $route->splat; + // Contains the contents of any '*' used in the URL pattern + $route->splat; }, true); ``` @@ -289,8 +294,8 @@ To map your own custom method, you use the `map` function: ```php // Map your method -Flight::map('hello', function($name){ - echo "hello $name!"; +Flight::map('hello', function ($name) { + echo "hello $name!"; }); // Call your custom method @@ -321,7 +326,7 @@ Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','p // Get an instance of your class // This will create an object with the defined parameters // -// new PDO('mysql:host=localhost;dbname=test','user','pass'); +// new PDO('mysql:host=localhost;dbname=test','user','pass'); // $db = Flight::db(); ``` @@ -332,8 +337,8 @@ new object. The callback function takes one parameter, an instance of the new ob ```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); +Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'), function (PDO $db): void { + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }); ``` @@ -362,8 +367,8 @@ by using the `map` method: ```php Flight::map('notFound', function(){ - // Display custom 404 page - include 'errors/404.html'; + // Display custom 404 page + include 'errors/404.html'; }); ``` @@ -390,8 +395,8 @@ methods as well as any custom methods that you've mapped. A filter function looks like this: ```php -function(&$params, &$output) { - // Filter code +function (array &$params, string &$output) { + // Filter code } ``` @@ -400,16 +405,16 @@ Using the passed in variables you can manipulate the input parameters and/or the You can have a filter run before a method by doing: ```php -Flight::before('start', function(&$params, &$output){ - // Do something +Flight::before('start', function (array &$params, string &$output) { + // Do something }); ``` You can have a filter run after a method by doing: ```php -Flight::after('start', function(&$params, &$output){ - // Do something +Flight::after('start', function (array &$params, string &$output) { + // Do something }); ``` @@ -420,20 +425,20 @@ Here's an example of the filtering process: ```php // Map a custom method -Flight::map('hello', function($name){ - return "Hello, $name!"; +Flight::map('hello', function ($name) { + return "Hello, $name!"; }); // Add a before filter -Flight::before('hello', function(&$params, &$output){ - // Manipulate the parameter - $params[0] = 'Fred'; +Flight::before('hello', function (array &$params, string &$output) { + // Manipulate the parameter + $params[0] = 'Fred'; }); // Add an after filter -Flight::after('hello', function(&$params, &$output){ - // Manipulate the output - $output .= " Have a nice day!"; +Flight::after('hello', function (array &$params, string &$output) { + // Manipulate the output + $output .= " Have a nice day!"; }); // Invoke the custom method @@ -442,26 +447,28 @@ echo Flight::hello('Bob'); This should display: - Hello Fred! Have a nice day! +``` +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: ```php -Flight::before('start', function(&$params, &$output){ - echo 'one'; +Flight::before('start', function (array &$params, string &$output){ + echo 'one'; }); -Flight::before('start', function(&$params, &$output){ - echo 'two'; +Flight::before('start', function (array &$params, string &$output): bool { + echo 'two'; - // This will end the chain - return false; + // This will end the chain + return false; }); // This will not get called -Flight::before('start', function(&$params, &$output){ - echo 'three'; +Flight::before('start', function (array &$params, string &$output){ + echo 'three'; }); ``` @@ -483,7 +490,7 @@ To see if a variable has been set you can do: ```php if (Flight::has('id')) { - // Do something + // Do something } ``` @@ -518,12 +525,14 @@ be reference like a local variable. Template files are simply PHP files. If the content of the `hello.php` template file is: ```php -Hello, ''! +Hello, ! ``` The output would be: - Hello, Bob! +``` +Hello, Bob! +``` You can also manually set view variables by using the set method: @@ -583,26 +592,26 @@ If the template files looks like this: ```php -
-