Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created October 2, 2025 14:30
Show Gist options
  • Save jpalala/2fdb113c15f6cc9583718f3e6b0b091e to your computer and use it in GitHub Desktop.
Save jpalala/2fdb113c15f6cc9583718f3e6b0b091e to your computer and use it in GitHub Desktop.

Revisions

  1. jpalala created this gist Oct 2, 2025.
    24 changes: 24 additions & 0 deletions bootstrap.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    // composer require usmanhalalit/pixie
    require 'vendor/autoload.php';

    use Pixie\Connection;
    use Pixie\QueryBuilder\QueryBuilderHandler;

    // Setup DB connection
    $config = [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'database' => 'mydb',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
    'collation' => 'utf8_general_ci',
    'prefix' => ''
    ];

    // Create a new connection
    new Connection('mysql', $config, 'QB');

    // Register Pixie into Flight
    Flight::set('db', new QueryBuilderHandler(new Connection('mysql', $config)));
    12 changes: 12 additions & 0 deletions sanctum-flight.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    Flight::before('route', function(){
    $authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
    if (preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
    $db = Flight::get('db');
    $token = $db->table('api_tokens')->where('token', $matches[1])->first();
    if (!$token) {
    Flight::halt(401, json_encode(["error" => "Invalid API token"]));
    }
    } else {
    Flight::halt(401, json_encode(["error" => "Missing Authorization header"]));
    }
    });
    30 changes: 30 additions & 0 deletions update-insert-fetch.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    ```Flight::route('GET /users', function() {
    $db = Flight::get('db');
    $users = $db->table('users')->get();
    Flight::json($users);
    });
    ```
    Insert user:

    ```pphp
    Flight::route('POST /users', function() {
    $db = Flight::get('db');
    $id = $db->table('users')->insert([
    'name' => 'Alice',
    'email' => '[email protected]',
    ]);
    Flight::json(['id' => $id]);
    });
    ```

    Update user:

    ```php
    Flight::route('PUT /users/@id', function($id) {
    $db = Flight::get('db');
    $db->table('users')->where('id', $id)->update([
    'name' => 'Updated Name'
    ]);
    Flight::json(['status' => 'ok']);
    });
    ```