Download the PHP package jivtesh/ci4-api-generator without Composer
On this page you can find all versions of the php package jivtesh/ci4-api-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jivtesh/ci4-api-generator
More information about jivtesh/ci4-api-generator
Files in jivtesh/ci4-api-generator
Package ci4-api-generator
Short Description Automatic API generator for CodeIgniter 4 based on database tables.
License BSD-3-Clause
Homepage https://github.com/jivtesh813/ci4-api-generator
Informations about the package ci4-api-generator
CodeIgniter 4 API Generator
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
- 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
- PHP 8.1 or higher
- CodeIgniter 4.x
- MySQL/MariaDB database
🚀 Installation
Install via Composer:
That's it! The package uses CodeIgniter's auto-discovery feature. Your APIs are now live at http://yoursite.com/api/v1/.
🎯 Quick Start
1. Install the Package
2. Configure Database
Make sure your database is configured in app/Config/Database.php:
3. Test Your APIs
Your APIs are now available! If you have a table named users, you can access:
4. View Available Routes
5. Generate API Documentation
This generates the file openapi.json in writable-->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
For each table in your database, the following endpoints are automatically created:
| Method | Endpoint | Description | Example |
|---|---|---|---|
| GET | /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)
By default, the package works with zero configuration. However, you can customize behavior by creating a config file.
Create Custom Configuration
- Create
app/Config/ApiGenerator.php:
Configuration Options
Basic Settings
Table Selection
Enable/Disable Endpoints Per Table
Restrict Visible Columns
Custom Validation Rules
Multi-Tenant Filtering
Dynamic Multi-Tenant Example:
Pagination Settings
Complete Configuration Example
🎨 API Usage Examples
Listing Records (GET)
Response:
Get Single Record (GET)
Single Primary Key
Response:
Composite Primary Key
For tables with composite primary keys (e.g., payments table with customerNumber and checkNumber):
Response:
Note: The order of primary key values in the URL must match the order defined in the database schema.
Create Record (POST)
Response:
Update Record (PUT)
Single Primary Key
Response:
Composite Primary Key
Response:
Delete Record (DELETE)
Single Primary Key
Response:
Composite Primary Key
Response:
🖥️ CLI Commands
List All Available APIs
Output:
List APIs for Specific Table
Generate API Documentation
Refresh Everything
📖 API Documentation
The package includes beautiful, interactive API documentation powered by Scalar.
Accessing Documentation
-
Generate the OpenAPI specification:
- Visit the documentation URL:
Features of Documentation
- Interactive Testing: Test APIs directly from the browser
- Request/Response Examples: See example requests and responses
- Schema Visualization: View table structures and relationships
- Authentication Testing: Built-in request builder
- Search Functionality: Quickly find endpoints
Customizing Documentation Path
Disabling Documentation
🔒 Security & Best Practices
Authentication & Authorization
This package provides the API infrastructure. You should add authentication:
-
Create an Authentication Filter (
app/Filters/ApiAuthFilter.php): - Register Filter in
app/Config/Filters.php:
Recommended Security Practices
- Always use HTTPS in production
- Implement rate limiting (use CodeIgniter's Throttle filter)
- Validate and sanitize all inputs (auto-handled by validation rules)
- Use strong validation rules for sensitive fields
- Hide sensitive columns using
enabledColumnsToShow - Restrict endpoints as needed via
enabledEndpoints - Implement proper CORS policies if accessed from browsers
- Use multi-tenant filtering for data isolation
🛠️ Advanced Usage
Dynamic Configuration
You can modify configuration at runtime:
Custom Validation Logic
Extend the ApiController for complex validation:
Soft Deletes Support
The package automatically detects deleted_at columns and enables soft deletes.
Timestamp Handling
Automatically detects and handles created_at and updated_at columns.
🐛 Troubleshooting
APIs Not Working
-
Check if composer autoload is updated:
-
Verify database connection:
-
Check routes are registered:
- Clear cache:
404 Errors
- Ensure mod_rewrite is enabled (Apache)
- Check
.htaccessfile exists in public folder - Verify
$baseURLinapp/Config/App.php
Validation Errors
- Check your custom validation rules syntax
- Ensure column names match database exactly
- Review auto-generated rules:
php spark api:generate
Performance Issues
- Enable route caching:
php spark api:generate --cache - Increase
maxCacheAgein configuration - Add database indexes on frequently queried columns
📊 Response Formats
Success Response
Error Responses
HTTP Status Codes
200 OK- Successful GET, PUT, DELETE201 Created- Successful POST400 Bad Request- Validation error404 Not Found- Record not found403 Forbidden- Endpoint disabled500 Internal Server Error- Server error
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📝 License
This package is open-sourced software licensed under the BSD-3-Clause license.
👤 Author
Jivtesh Ghatora
- Email: [email protected]
- GitHub: @jivtesh813
🙏 Acknowledgments
- Built for CodeIgniter 4 framework
- Uses Scalar for beautiful API documentation
- Inspired by the need for rapid API development
Star ⭐ this repository if you find it helpful!