PHP code example of masroore / flight

1. Go to this page and download the library: Download masroore/flight library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

masroore / flight example snippets




Flight::route('/', function(){
    echo 'hello world!';
});

Flight::start();





Flight::route('/', function(){
    echo 'hello world!';
});

Flight::start();

Flight::route('/', function(){
    echo 'hello world!';
});

function hello(){
    echo 'hello world!';
}

Flight::route('/', 'hello');

class Greeting {
    public static function hello() {
        echo 'hello world!';
    }
}

Flight::route('/', array('Greeting', 'hello'));

class Greeting
{
    public function __construct() {
        $this->name = 'John Doe';
    }

    public function hello() {
        echo "Hello, {$this->name}!";
    }
}

$greeting = new Greeting();

Flight::route('/', array($greeting, 'hello')); 

Flight::route('GET /', function(){
    echo 'I received a GET request.';
});

Flight::route('POST /', function(){
    echo 'I received a POST request.';
});

Flight::route('GET|POST /', function(){
    echo 'I received either a GET or a POST request.';
});

Flight::route('/user/[0-9]+', function(){
    // This will match /user/1234
});

Flight::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";
});

Flight::route('/@name/@id:[0-9]{3}', function($name, $id){
    // This will match /bob/123
    // But will not match /bob/12345
});

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
});

Flight::route('/blog/*', function(){
    // This will match /blog/2000/02/01
});

Flight::route('*', function(){
    // Do something
});

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
});

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);

// Map your method
Flight::map('hello', function($name){
    echo "hello $name!";
});

// Call your custom method
Flight::hello('Bob');

// Register your class
Flight::register('user', 'User');

// Get an instance of your class
$user = Flight::user();

// 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();

// 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);
});

// Shared instance of the class
$shared = Flight::db();

// New instance of the class
$new = Flight::db(false);

Flight::map('notFound', function(){
    // Display custom 404 page
    

// Register your custom class
Flight::register('router', 'MyRouter');

// When Flight loads the Router instance, it will load your class
$myrouter = Flight::router();

function(&$params, &$output) {
    // Filter code
}

Flight::before('start', function(&$params, &$output){
    // Do something
});

Flight::after('start', function(&$params, &$output){
    // Do something
});

// Map a custom method
Flight::map('hello', function($name){
    return "Hello, $name!";
});

// Add a before filter
Flight::before('hello', function(&$params, &$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!";
});

// Invoke the custom method
echo Flight::hello('Bob');

Flight::before('start', function(&$params, &$output){
    echo 'one';
});

Flight::before('start', function(&$params, &$output){
    echo 'two';

    // This will end the chain
    return false;
});

// This will not get called
Flight::before('start', function(&$params, &$output){
    echo 'three';
});

// Save your variable
Flight::set('id', 123);

// Elsewhere in your application
$id = Flight::get('id');

if (Flight::has('id')) {
     // Do something
}

// Clears the id variable
Flight::clear('id');

// Clears all variables
Flight::clear();

Flight::set('flight.log_errors', true);

Flight::render('hello.php', array('name' => 'Bob'));

Hello, ' echo $name; 

Flight::view()->set('name', 'Bob');

Flight::render('hello');

Flight::set('flight.views.path', '/path/to/views');

Flight::render('header', array('heading' => 'Hello'), 'header_content');
Flight::render('body', array('body' => 'World'), 'body_content');

Flight::render('layout', array('title' => 'Home Page'));

<h1> echo $heading; 

<div> echo $body; 

<html>
<head>
<title> echo $title; 

// Load Smarty library
 the view class
// Also pass a callback function to configure Smarty on load
Flight::register('view', 'Smarty', array(), function($smarty){
    $smarty->template_dir = './templates/';
    $smarty->compile_dir = './templates_c/';
    $smarty->config_dir = './config/';
    $smarty->cache_dir = './cache/';
});

// Assign template data
Flight::view()->assign('name', 'Bob');

// Display the template
Flight::view()->display('hello.tpl');

Flight::map('render', function($template, $data){
    Flight::view()->assign($data);
    Flight::view()->display($template);
});

Flight::map('error', function(Exception $ex){
    // Handle error
    echo $ex->getTraceAsString();
});

Flight::set('flight.log_errors', true);

Flight::map('notFound', function(){
    // Handle not found
});

Flight::redirect('/new/location');

Flight::redirect('/new/location', 401);

$request = Flight::request();

$id = Flight::request()->query['id'];

$id = Flight::request()->query->id;

$body = Flight::request()->getBody();

$id = Flight::request()->data->id;

Flight::route('/news', function(){
    Flight::lastModified(1234567890);
    echo 'This content will be cached.';
});

Flight::route('/news', function(){
    Flight::etag('my-unique-id');
    echo 'This content will be cached.';
});

Flight::halt();

Flight::halt(200, 'Be right back...');

Flight::stop();

Flight::json(array('id' => 123));

Flight::jsonp(array('id' => 123), 'q');

Flight::set('flight.log_errors', true);

Flight::map($name, $callback) // Creates a custom framework method.
Flight::register($name, $class, [$params], [$callback]) // Registers a class to a framework method.
Flight::before($name, $callback) // Adds a filter before a framework method.
Flight::after($name, $callback) // Adds a filter after a framework method.
Flight::path($path) // Adds a path for autoloading classes.
Flight::get($key) // Gets a variable.
Flight::set($key, $value) // Sets a variable.
Flight::has($key) // Checks if a variable is set.
Flight::clear([$key]) // Clears a variable.
Flight::init() // Initializes the framework to its default settings.
Flight::app() // Gets the application object instance

Flight::start() // Starts the framework.
Flight::stop() // Stops the framework and sends a response.
Flight::halt([$code], [$message]) // Stop the framework with an optional status code and message.
Flight::route($pattern, $callback) // Maps a URL pattern to a callback.
Flight::redirect($url, [$code]) // Redirects to another URL.
Flight::render($file, [$data], [$key]) // Renders a template file.
Flight::error($exception) // Sends an HTTP 500 response.
Flight::notFound() // Sends an HTTP 404 response.
Flight::etag($id, [$type]) // Performs ETag HTTP caching.
Flight::lastModified($time) // Performs last modified HTTP caching.
Flight::json($data, [$code], [$encode], [$charset], [$option]) // Sends a JSON response.
Flight::jsonp($data, [$param], [$code], [$encode], [$charset], [$option]) // Sends a JSONP response.



use flight\Engine;

$app = new Engine();

$app->route('/', function(){
    echo 'hello world!';
});

$app->start();

server {
    location / {
        try_files $uri $uri/ /index.php;
    }
}