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.
<?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)));
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"]));
}
});
    $db = Flight::get('db');
    $users = $db->table('users')->get();
    Flight::json($users);
});

Insert user:

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:

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']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment