# solidphp **Repository Path**: wfdaj/solidphp ## Basic Information - **Project Name**: solidphp - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-15 - **Last Updated**: 2024-07-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SolidPHP SolidPHP is the most `lightweight` and `efficient` PHP Micro Framework. #### table of content - [Docs & Features](#docs--features) - [1. API Handler](#1-api-handler) - [2. Middleware](#2-middleware) - [3. JWT Auth](#3-jwt-auth) - [4. Database Helpers](#4-database-helpers) - [5. CSRF Protection](#5-csrf-protection) - [6. Templating Engine](#6-templating-engine) - [7. Flash Message](#7-flash-message) - [8. Url Encryption](#8-url-encryption) - [9. React Adaptor](#9-react-adaptor) - [10. More](#10-more) - [How to Use](#how-to-use) - [Credits](#credits) ## Docs & Features ### 1. API Handler ```php use SolidPHP\Router; $app = new Router(); $app->any('/testing', function($request,$response){ return $response->send("ANY request"); }); $app->get('/user', function($request,$response){ return $response->json(["id"=>"1","name"=>"John"],200); }); $app->post('/:name/:id', function ($req, $res) { return $res->json(["id" => $req["params"]["id"], "name" => $req["params"]["name"]], 200); }); $app->put('/', function($request,$response){ return $response->send("PUT request"); }); $app->patch('/', function($request,$response){ return $response->send("PATCH request"); }); $app->delete('/', function($request,$response){ return $response->send("DELETE request"); }); $app->error(function(Exception $e,$response){ return $response->send('path not found',404); }); $app->start(); ``` ### 2. Middleware ```php // def function middleware1($req, $res, $next) { $res->send("Middleware 1 executed"); $next(); } function middleware2($req, $res, $next) { $res->send("Middleware 2 executed"); $next(); } // use $app->get('/user', 'middleware1', 'middleware2', function($request,$response){ return $response->json(["id"=>"1","name"=>"John"],200); }); ``` ### 3. JWT Auth ```php use SolidPHP\JWT; JWT::setSecretKey('12345678'); // middleware function useJwtAuth($req, $res, $next) { $jwtToken = JWT::getToken(); $isValid = JWT::verify($jwtToken); // ...... } $app->post('/login', function ($req, $res) { // ...... $token = JWT::generate(['id' => 1, 'name' => 'admin'], 2592000); // day in second JWT::setCookie($token); return $res->json(['status' => 'success', 'token' => $token]) // ..... }); // protected page, use middleware $app->get('/person', 'useJwtAuth', function ($req, $res) use ($db) { // ..... }); $app->get('/logout', function ($req, $res) { JWT::deleteCookie(); // ...... }); ``` ### 4. Database Helpers ```php /** * Perform simplified, secure, original MySQLi database operations with defined fields and data types. * * @method mixed query($query) — Original mysqli query() * @method mixed prepare($query) — Original mysqli prepare() * @method mixed bind_param($tableName, ...$params) — Modied mysqli bind_param() * @method mixed execute() — Original mysqli execute() * @method mixed close() — Original mysqli $stmt close() * @method mixed shutdown() — Original mysqli close() connection * @method mixed getStmt() — Original mysqli $stmt * @method mixed get_result() — Original mysqli get_result() * @method mixed table($tableName, array $fields) — Define the table for efficiency * @method string fields($tableName) — Get fields name based on defined table. * @method string setClause($tableName) — Generate SET Clause based on defined table. */ use SolidPHP\DBMysql; // init $db = new DBMysql([ 'host' => 'localhost', 'username' => 'your_username', 'password' => 'your_password', 'database' => 'your_database', ]); /** * Define the table for efficiency and bind_param() purposes. * @param string $tableName * @param array $fields['field1' => 'type1'] * - type 'i' is used for an integer (123) * - type 'd' is used for a double (3.14) * - type 's' is used for a string ('sample') * - type 'b' is used for binary data (file_get_content()) */ $db->table('persons', [ 'name' => 's', 'age' => 'i' ]); // use // ({$db->fields('persons')}) => use defined table fields // bind_param('persons' => use defined data type $db->prepare("INSERT INTO persons ({$db->fields('persons')}) VALUES (?, ?)") ->bind_param('persons', $req["body"]["name"], $req["body"]["age"]) ->execute(); // use // {$db->setClause('persons')} => get set clause from defined table fields // ->bind_param('sii' => use raw data type $db->prepare("UPDATE persons SET {$db->setClause('persons')} WHERE id = ?") ->bind_param('sii', $req["body"]["name"], $req["body"]["age"], $cryptor->decrypt($req["params"]["id"])) ->execute(); ``` ### 5. CSRF Protection ```html use SolidPHP\CSRF;
$app->post('/person', function ($req, $res) use ($db) { CSRF::verify($req); // ..... }); ``` ### 6. Templating Engine routes ```php $app->get('/profile', 'middleware1', function ($req, $res) { // param: view page, data $res->phpView(__DIR__ . '/views/pages/profile.php', ['name' => 'John']); }); ``` layout.php ```htmlWelcome to the Profile page!
My name is = $name ?>
``` ### 7. Flash Message ```php use SolidPHP\Flash; // set Flash::set('message', 'data successfully added!'); // use