Create a .env file in the root directory with the following configuration:
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
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tlsStart the PHP development server locally:
php -S localhost:8000http://localhost:8000
GET /- Base endpoint
- PHP 8.1 or higher
- MySQL/MariaDB
- Composer (for dependency management)
- Clone the repository
- Copy
.env.exampleto.envand configure your environment variables - Install dependencies:
composer install- Ensure your
.envfile is included in.gitignore - API key authentication is required for all endpoints
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();- Create new controller files in
app/controllers/ - 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 value- Additional rules can be added in the Validator class
The API returns JSON responses in the following format:
Success Response:
{
"success": true,
"data": {
// Response data
}
}Error Response:
{
"success": false,
"error": "Error message",
"code": 400
}Use the console utility to generate new controllers and models:
# Create a new controller
./console controller YourControllerName
# Create a new model
./console model YourModelNameThe controller command will:
- Create a new controller in
app/Controllers/YourControllerName.php - Automatically create a corresponding model in
app/Models/YourModelName.php - Set up basic CRUD method templates
- Configure proper namespacing and type declarations
The model command will:
- Create a new model in
app/Models/YourModelName.php - Set up database table connection
- Configure basic CRUD methods
Example:
./console controller ProductCreates:
app/Controllers/ProductController.phpapp/Models/Product.php
./console model CategoryCreates:
app/Models/Category.php
To create a new table,
- first create a new model using the
./console modelcommand. - then add the table name and columns to the model.
- for a single table use,
public const TABLE_NAME = 'TableName';
public const TABLE_COLUMNS = [] - for a multiple table use,
public const TABLE1_TABLE = 'TableName';
public const TABLE1_COLUMNS = [];
public const TABLE2_TABLE = 'TableName2';
public const TABLE2_COLUMNS = [];- then create a new seeding using the
./console seedcommand.
./console seed YourSeedingName