Created
October 2, 2025 14:30
-
-
Save jpalala/2fdb113c15f6cc9583718f3e6b0b091e to your computer and use it in GitHub Desktop.
Revisions
-
jpalala created this gist
Oct 2, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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))); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"])); } }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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']); }); ```