DB_HOST=localhost DB_NAME=your_database DB_USER=your_username DB_PASS=your_password API_KEY=your_api_key MAIL_HOST=smtp.gmail.com MAIL_PORT=587 [email protected] MAIL_PASSWORD=your_email_password MAIL_ENCRYPTION=tls
Start the PHP development server locally:
php -S localhost:8000
http://localhost:8000
composer install
Routes are defined in routes/api.php
using the Router class:
use Utility\Core\Router; $router = new Router(); // Define routes $router->get('endpoint/{parameter}', 'ControllerName@methodName'); $router->post('endpoint', 'ControllerName@methodName'); $router->put('endpoint/{parameter}', 'ControllerName@methodName'); $router->delete('endpoint/{parameter}', 'ControllerName@methodName'); // Handle the request $router->handleRequest();
1. Create new controller files in app/controllers/
2. Basic controller structure:
<?php declare(strict_types=1); namespace App\Controllers; use Utility\Core\Response; use Utility\Core\Validator; class YourController { private YourModel $model; public function __construct() { $this->model = new YourModel(); } // GET request handler public function getItem(?string $id = null): void { try { // Handle both single item and collection requests $result = $id ? $this->model->getItem($id) : $this->model->getAllItems(); Response::successResponse($result, 200); } catch (Exception $e) { Response::errorResponse($e->getMessage(), 500); } } // POST request handler public function createItem(): void { try { // Validate incoming data Validator::validate($_POST, [ "field1" => "required", "field2" => "required|email" ]); $result = $this->model->createItem($_POST); Response::successResponse(["message" => "Item created"], 200); } catch (Exception $e) { Response::errorResponse($e->getMessage(), 400); } } }
Available validation rules for use with Validator::validate()
:
required
: Field must be present and not emptyemail
: Must be a valid email formatstring
: Must be a string valueThe API returns JSON responses in the following format:
Success Response:
{ "success": true, "data": { // Response data } }
Error Response:
{ "success": false, "error": "Error message", "code": 400 }