PHP Classes

How to Implement a PHP CRUD API Automatically for MySQL based Applications Using the Package PHP CRUD API Generator: Create an API to access MySQL database record

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-05-20 (14 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
php-crud-api-generat 1.0MIT/X Consortium ...8Databases, Libraries, Web services, P...
Description 

Author

This package can create an API to access MySQL database record.

It can take configuration PHP scripts that define details of execution of a REST API that can take HTTP requests and execute MySQL database access SQL queries to manipulate the table records.

The package can automatically discover MySQL database tables and columns to provide an API that can execute common CRUD operations like creating, retrieve, updating, delete and listing table records.

It can also provide API metadata in OpenAPI JSON format to help to generate documentation.

Picture of Adrian M
  Performance   Level  
Name: Adrian M <contact>
Classes: 5 packages by
Country: Ireland Ireland

Instructions

Details

PHP CRUD API Generator

Expose your MySQL/MariaDB database as a secure, flexible, and instant REST-like API. Features optional authentication (API key, Basic Auth, JWT, OAuth-ready), OpenAPI (Swagger) docs, and zero code generation.

? Features

  • Auto-discovers tables and columns
  • Full CRUD endpoints for any table
  • Configurable authentication (API Key, Basic Auth, JWT, or none)
  • OpenAPI (Swagger) JSON endpoint for instant docs
  • Clean PSR-4 codebase
  • PHPUnit tests and extensible architecture

? Installation

composer create-project yourvendor/php-crud-api-generator

?? Configuration

Copy and edit config files:

cp config/db.example.php config/db.php
cp config/api.example.php config/api.php

Edit config/db.php:

return [
    'host' => 'localhost',
    'dbname' => 'your_database',
    'user' => 'your_db_user',
    'pass' => 'your_db_password',
    'charset' => 'utf8mb4'
];

Edit config/api.php:

return [
    'auth_enabled' => false, // true to require authentication
    'auth_method' => 'apikey', // 'apikey', 'basic', 'jwt', 'oauth'
    'api_keys' => ['changeme123'], // API keys for 'apikey'
    'basic_users' => ['admin' => 'secret'], // Users for 'basic' and 'jwt'
    'jwt_secret' => 'YourSuperSecretKey',
    'jwt_issuer' => 'yourdomain.com',
    'jwt_audience' => 'yourdomain.com',
    'oauth_providers' => [
        // 'google' => ['client_id' => '', 'client_secret' => '', ...]
    ]
];

? Authentication Modes

  • No auth: `'auth_enabled' => false`
  • API Key: `'auth_enabled' => true, 'auth_method' => 'apikey'` Client: `X-API-Key` header or `?api_key=...`
  • Basic Auth: `'auth_method' => 'basic'` Client: HTTP Basic Auth
  • JWT: `'auth_method' => 'jwt'` 1. `POST /index.php?action=login` with `username` and `password` (from `basic_users`) 2. Use returned token as `Authorization: Bearer <token>`
  • OAuth (future): `'auth_method' => 'oauth'` (Implement provider logic as needed)

? API Endpoints

All requests go through public/index.php with action parameter.

| Action | Method | Usage Example | |-----------|--------|------------------------------------------------------------| | tables | GET | /index.php?action=tables | | columns | GET | /index.php?action=columns&table=users | | list | GET | /index.php?action=list&table=users | | read | GET | /index.php?action=read&table=users&id=1 | | create | POST | /index.php?action=create&table=users (form POST) | | update | POST | /index.php?action=update&table=users&id=1 (form POST) | | delete | POST | /index.php?action=delete&table=users&id=1 | | openapi | GET | /index.php?action=openapi | | login | POST | /index.php?action=login (JWT only) |

? Example curl Commands

curl http://localhost/index.php?action=tables
curl -H "X-API-Key: changeme123" "http://localhost/index.php?action=list&table=users"
curl -X POST -d "username=admin&password=secret" http://localhost/index.php?action=login
curl -H "Authorization: Bearer <token>" "http://localhost/index.php?action=list&table=users"
curl -u admin:secret "http://localhost/index.php?action=list&table=users"

?? Security Notes

  • Enable authentication for any public deployment!
  • Never commit real credentials?use `.gitignore` and example configs.
  • Restrict DB user privileges.

? Running Tests

./vendor/bin/phpunit

?? Roadmap

  • RESTful route aliases (`/users/1`)
  • OAuth2 provider integration
  • More DB support (Postgres, SQLite)
  • Pagination, filtering, relations

? License

MIT

? Credits

Built by BitHost. PRs/issues welcome!


  Files folder image Files (16)  
File Role Description
Files folder imageconfig (2 files)
Files folder imagepublic (1 file)
Files folder imagesrc (6 files)
Files folder imagetests (1 file)
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (16)  /  config  
File Role Description
  Accessible without login Plain text file api.example.php Aux. Configuration script
  Accessible without login Plain text file db.example.php Aux. Configuration script

  Files folder image Files (16)  /  public  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (16)  /  src  
File Role Description
  Accessible without login Plain text file ApiGenerator.php Class Class source
  Accessible without login Plain text file Authenticator.php Class Class source
  Accessible without login Plain text file Database.php Class Class source
  Accessible without login Plain text file OpenApiGenerator.php Class Class source
  Accessible without login Plain text file Router.php Class Class source
  Accessible without login Plain text file SchemaInspector.php Class Class source

  Files folder image Files (16)  /  tests  
File Role Description
  Accessible without login Plain text file ApiGeneratorTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0