Skip to content

using Flywheel with Flight php micro framework

Marat Bedoev edited this page Jan 18, 2016 · 1 revision

to get flywheel working with flight php framework, you need to register custom class in flight, so you can access it everywhere. First load flywheel and flight with composer
$ composer require jamesmoss/flywheel
$ composer require mikecao/flight

<?php
require 'vendor/autoload.php';  

// this is how regular flywheel configuration looks like:
// you can remove this rows later, they are just as example
$config = new \JamesMoss\Flywheel\Config('db');
$repo = new \JamesMoss\Flywheel\Repository('posts', $config );  

// registering flywheel in flight:
// as you can see, we use the storage folder config as funtion argument for registering posts class
Flight::register('posts','\JamesMoss\Flywheel\Repository',array('posts', new \JamesMoss\Flywheel\Config('db')));  
// this is equal to: $repo = new \JamesMoss\Flywheel\Repository('posts', new \JamesMoss\Flywheel\Config('db'));  

Flight::route('/posts/', function()
{
    // here we can get our flywheel repository by usig Flight::posts()
    $posts = Flight::posts()->query()->execute();
    echo "<pre>";
    foreach($posts as $post)
    {
        var_dump($post);
    }
    echo "</pre>";
});  

Flight::route('/', function()
{
    $post = new \JamesMoss\Flywheel\Document(array(
        'title'     => 'An introduction to Flywheel',
        'dateAdded' => new \DateTime('2016-10-10'),
        'body'      => 'A lightweight, flat-file, document database for PHP...',
        'wordCount' => 7,
    ));
    $id = Flight::serien()->store($post);
    echo $id;
});  

Flight::start();

just copy this code and save an index.php and create writable folders db/posts/
don't forget the htaccess code from flight installation guide
http://flightphp.com/install

Open your url in browser and refresh the page 2-3 times to get demo data.
Then open /posts/ and you will see your posts.
you could also register \JamesMoss\Flywheel\Document if you want, this is just a demo for beginners to get started.

cheers

Clone this wiki locally