Skip to content

fegig/PHP_Crud_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic API With PHP

Setup

Environment Configuration

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=tls

Local Development

Start the PHP development server locally:

php -S localhost:8000

API Documentation

Base URL

http://localhost:8000

Available Endpoints

  • GET / - Base endpoint

Requirements

  • PHP 8.1 or higher
  • MySQL/MariaDB
  • Composer (for dependency management)

Installation

  1. Clone the repository
  2. Copy .env.example to .env and configure your environment variables
  3. Install dependencies:
composer install

Security

  • Ensure your .env file is included in .gitignore
  • API key authentication is required for all endpoints

API Development Guide

Router Configuration

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();

Creating Controllers

  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);
        }
    }
}

Validation Rules

Available validation rules for use with Validator::validate():

  • required: Field must be present and not empty
  • email: Must be a valid email format
  • string: Must be a string value
  • Additional rules can be added in the Validator class

Response Format

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
}

Console Commands

Creating Controllers and Models

Use the console utility to generate new controllers and models:

# Create a new controller
./console controller YourControllerName

# Create a new model
./console model YourModelName

The 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 Product

Creates:

  • app/Controllers/ProductController.php
  • app/Models/Product.php
./console model Category

Creates:

  • app/Models/Category.php

To create a new table,

  • first create a new model using the ./console model command.
  • 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 seed command.
./console seed YourSeedingName

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors