This repository provides a comprehensive example of building a RESTful API using the Luminova PHP framework, showcasing both HTTP and CLI implementations. The example is designed to help developers understand how to create APIs that support full CRUD operations (Create, Read, Update, Delete) and handle various HTTP methods such as GET, POST, PUT, and DELETE. It also supports secure content delivery through the Luminova\Storages\FileDelivery class. This feature enables serving images stored in private directories directly via URL access, eliminating the need for creating symbolic links (symlinks).
In addition to HTTP-based API endpoints, this example also demonstrates the power of Luminova's command-line tools, making it possible to perform API-related tasks directly via the CLI. Whether you're looking to seed your database, manage user tokens, or perform backend operations, this repository covers it all.
By following the provided steps, you can quickly set up an API infrastructure, learn best practices for working with Luminova, and explore features like database migrations, API client authentication, and middleware for rate-limiting or token validation.
This project is ideal for developers aiming to:
- Build scalable and secure APIs for web and mobile applications.
- Explore the seamless integration of HTTP and CLI workflows.
- Learn Luminova's approach to rapid API development and management.
Below is an overview of the primary files involved in the API implementation, organized by functionality and purpose. Each file plays a critical role in building a RESTful API using the Luminova PHP framework.
Controllers handle HTTP and CLI requests, providing routes and business logic for the API.
-
App\Controllers\Http\RestController
Implements the core API endpoints for CRUD operations. This class handles user authentication, input validation, and database interaction for the/api/v1/postsroute. -
App\Controllers\Http\Welcome
Serves as the main page controller, providing access to the API's landing page. It also manages private file delivery via theLuminova\Storages\FileDeliveryclass.
- App\Controllers\Cli\PostsCommand
Implements command-line interactions with the API, managing posts and handling API keys for clients.
Define the database schema for the API's core entities.
-
App\Database\Migrations\PostsMigration
Defines the schema for thepoststable, including fields fortitle,body, and relationships with users. -
App\Database\Migrations\UserMigration
Defines the schema for theuserstable, managing user authentication and profile data.
Populate the database with initial data.
-
App\Database\Seeders\PostsSeeder
Inserts sample post data for testing and development purposes. -
App\Database\Seeders\UserSeeder
Inserts sample user data, including admin and regular user roles.
Models provide a programmatic interface for interacting with the database tables.
-
App\Models\Posts
Represents thepoststable, including relationships to users and validation logic for creating or updating posts. -
App\Models\Users
Represents theuserstable, managing user-specific operations such as authentication and role assignments.
- App\Config\Apis
Central configuration file for defining HTTP API-related behaviors, such asallowCredentials,allowOrigins, andallowHeaders.
Clone the repository using Git or download the files directly from GitHub:
cd your-project-path
git clone https://github.com/luminovang/luminova-rest-api-example.gitNavigate to the project directory and update the Composer dependencies:
composer updateConfigure your MySQL socket path in the .env file:
database.mysql.socket.path = /var/mysql/mysql.sockUse the Luminova built-in development server or a third-party server such as XAMPP or WAMPP.
Apply database migrations to create the required tables:
php novakit db:migratePopulate the database with sample data:
php novakit db:seedGenerate an API token for your client:
Example:
cd public
php index.php posts key --user-id=1 --quota=1000 --expiry=3600Copy the generated API token for use with API tools like Postman or curl.
Retrieve all post contents.
curl --location 'https://localhost/your-project-path/public/api/v1/posts' \
--header 'X-Api-Client-Id: <your-client-id>' \
--header 'Authorization: <your-api-token>'For more examples and details, refer to the documentation in this repository.
Retrieve a single post content by id.
curl --location 'https://localhost/your-project-path/public/api/v1/posts/<post-id>' \
--header 'X-Api-Client-Id: <your-client-id>' \
--header 'Authorization: <your-api-token>'Create a new post with an optional image.
curl --location 'https://localhost/your-project-path/public/api/v1/posts/create' \
--header 'X-Api-Client-Id: <your-client-id>' \
--header 'Authorization: <your-api-token>' \
--form 'body="{
\"userId\": 10,
\"title\": \"This a test new title\",
\"body\": \"New Body content\"
}"' \
--form 'image=@"/Path/To/Images/image.png"'Update a existing post with an optional image.
curl --location --request PUT 'https://localhost/your-project-path/public/api/v1/posts/update/<post-id>' \
--header 'X-Api-Client-Id: <your-client-id>' \
--header 'Authorization: <your-api-token>' \
--form 'body="{
\"title\": \"New Update a test new title\",
\"body\": \"New Update Body content\"
}"'Delete a existing post.
curl --location --request DELETE 'https://localhost/your-project-path/public/api/v1/posts/delete/<post-id>' \
--header 'X-Api-Client-Id: <your-client-id>' \
--header 'Authorization: <your-api-token>'First navigate to public directory of your project.
Lists users with optional pagination.
php index.php posts users --limit=10 --offset=0Lists posts with optional pagination.
php index.php posts list --limit=2For more, run help command to show all available post commands:
php index.php posts --help