PHPackages                             jivtesh/ci4-api-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. jivtesh/ci4-api-generator

ActiveLibrary[HTTP &amp; Networking](/categories/http)

jivtesh/ci4-api-generator
=========================

Automatic API generator for CodeIgniter 4 based on database tables.

v1.0.3(5mo ago)4123[1 issues](https://github.com/jivtesh813/ci4-api-generator/issues)[1 PRs](https://github.com/jivtesh813/ci4-api-generator/pulls)BSD-3-ClausePHPPHP &gt;=8.1

Since Nov 23Pushed 5mo agoCompare

[ Source](https://github.com/jivtesh813/ci4-api-generator)[ Packagist](https://packagist.org/packages/jivtesh/ci4-api-generator)[ Docs](https://github.com/jivtesh813/ci4-api-generator)[ RSS](/packages/jivtesh-ci4-api-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

CodeIgniter 4 API Generator
===========================

[](#codeigniter-4-api-generator)

[![PHP Version](https://camo.githubusercontent.com/7663c9d53dc13cedaf0660a8745a7e77d2dd711257f36aa86ebce12a0600ef42/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d626c75652e737667)](https://php.net)[![CodeIgniter](https://camo.githubusercontent.com/defe13e820bf7a6e6ed3a0e4ea9c5e80699079db49d76b24c296d0d6f88fadae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f646549676e697465722d253545342e302d6f72616e67652e737667)](https://codeigniter.com)[![License](https://camo.githubusercontent.com/3330c5849986f248a756c19d133401d05b0d249148b7d41eeb6648cebe21136c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d2d332d2d436c617573652d677265656e2e737667)](LICENSE)

Automatically generate RESTful APIs for your CodeIgniter 4 application based on your MySQL database tables. No code required! Just install, configure your database, and your APIs are ready to use.

✨ Features
----------

[](#-features)

- **Instant API Generation**: APIs work immediately after installation - no additional setup required
- **Full CRUD Operations**: Automatic GET, POST, PUT, DELETE endpoints for all tables
- **Composite Primary Key Support**: Automatically handles tables with single or multiple primary keys
- **Smart Validation**: Auto-generates validation rules from database schema
- **OpenAPI/Swagger Documentation**: Beautiful interactive API documentation with Scalar UI
- **Pagination Support**: Built-in pagination for listing endpoints
- **Advanced Filtering**: Filter records by any column via query parameters
- **Multi-tenant Support**: Built-in column filtering for multi-tenant applications
- **Flexible Configuration**: Customize everything - endpoints, columns, validation rules per table
- **Foreign Key Support**: Automatically detects relationships
- **Route Caching**: Fast performance with intelligent route caching
- **CLI Commands**: Powerful commands to manage and inspect your APIs

📋 Requirements
--------------

[](#-requirements)

- PHP 8.1 or higher
- CodeIgniter 4.x
- MySQL/MariaDB database

🚀 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require jivtesh/ci4-api-generator
```

That's it! The package uses CodeIgniter's auto-discovery feature. Your APIs are now live at `http://yoursite.com/api/v1/`.

🎯 Quick Start
-------------

[](#-quick-start)

### 1. Install the Package

[](#1-install-the-package)

```
composer require jivtesh/ci4-api-generator
```

### 2. Configure Database

[](#2-configure-database)

Make sure your database is configured in `app/Config/Database.php`:

```
public array $default = [
    'hostname' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password',
    'database' => 'your_database',
    'DBDriver' => 'MySQLi',
    // ... other settings
];
```

### 3. Test Your APIs

[](#3-test-your-apis)

Your APIs are now available! If you have a table named `users`, you can access:

```
# List all users
GET http://yoursite.com/api/v1/users

# Get a specific user
GET http://yoursite.com/api/v1/users/1

# Create a new user
POST http://yoursite.com/api/v1/users

# Update a user
PUT http://yoursite.com/api/v1/users/1

# Delete a user
DELETE http://yoursite.com/api/v1/users/1
```

### 4. View Available Routes

[](#4-view-available-routes)

```
php spark api:list
```

### 5. Generate API Documentation

[](#5-generate-api-documentation)

```
php spark api:generate --openapi
```

This generates the file openapi.json in writable--&gt;api-docs folderm which is used by Scalar in View file to create api documentation.

Then visit `http://yoursite.com/api/v1/docs` to see your interactive API documentation.

📚 Available Endpoints
---------------------

[](#-available-endpoints)

For each table in your database, the following endpoints are automatically created:

MethodEndpointDescriptionExampleGET`/api/v1/{table}`List all records (paginated)`/api/v1/users`GET`/api/v1/{table}/{id}`Get a single record`/api/v1/users/1`GET`/api/v1/{table}/{id1}/{id2}`Get a single record (composite key)`/api/v1/payments/103/HQ336336`POST`/api/v1/{table}`Create a new record`/api/v1/users`PUT`/api/v1/{table}/{id}`Update a record`/api/v1/users/1`PUT`/api/v1/{table}/{id1}/{id2}`Update a record (composite key)`/api/v1/payments/103/HQ336336`DELETE`/api/v1/{table}/{id}`Delete a record`/api/v1/users/1`DELETE`/api/v1/{table}/{id1}/{id2}`Delete a record (composite key)`/api/v1/payments/103/HQ336336`**Notes**:

- Table names with underscores are converted to hyphens in URLs (e.g., `user_profiles` → `/api/v1/user-profiles`)
- The package automatically detects composite primary keys and generates appropriate routes
- For composite keys, provide all key values in order as defined in the database

🔧 Configuration (Optional)
--------------------------

[](#-configuration-optional)

By default, the package works with zero configuration. However, you can customize behavior by creating a config file.

### Create Custom Configuration

[](#create-custom-configuration)

1. Create `app/Config/ApiGenerator.php`:

```
