Download the PHP package nihad1213/litephp without Composer
On this page you can find all versions of the php package nihad1213/litephp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nihad1213/litephp
More information about nihad1213/litephp
Files in nihad1213/litephp
Package litephp
Short Description A lightweight PHP framework for building REST APIS.
License MIT
Informations about the package litephp
LitePHP Framework
LitePHP is a lightweight PHP framework designed for building simple and efficient APIs. It provides a straightforward CLI tool for common development tasks and a minimalist structure for rapid API development.
Installation
Get started with LitePHP using Composer:
This will install LitePHP, navigate to your new project directory, copy .env.example file to .env, genereate JWT_SECRET key and start the built-in PHP development server.
CLI Commands
LitePHP comes with a built-in command-line interface to help you manage your development workflow. Here are the available commands:
Start Development Server
Launches the PHP built-in development server at http://localhost:8000, serving files from the 'api' directory.
Generate JWT Secret Key
Creates or updates the JWT_SECRET in your .env file with a secure random key (32 bytes). This is essential for JWT-based authentication in your API.
Create a Controller
Scaffolds a new controller in the 'controllers' directory with RESTful methods (getAll, getOne, create, update, delete) and basic validation.
Create a Gateway
Scaffolds a new gateway in the 'gateways' directory. Gateways handle database operations for specific entities, providing a clean separation between controllers and database logic.
Run Database Migrations
Executes all SQL files in the 'database' directory in alphabetical order, allowing you to set up and modify your database schema.
Project Structure
Login Route Integration
To add authentication routes to your LitePHP application, you can integrate custom routes in your main index.php file. Here's an example of how to add a login route that bypasses normal authentication:
This allows you to create dedicated authentication endpoints that don't require existing authentication, such as login forms or token generation endpoints. Place this code in your main routing logic before other route processing occurs.
Key Components
Controllers
Controllers handle HTTP requests and return appropriate responses. They are responsible for:
- Processing incoming requests
- Validating input data
- Interacting with gateways to perform data operations
- Returning appropriate HTTP responses
Each controller extends the BaseController class, which provides common response methods and utilities for handling API requests.
Controller Example
Attribute-Based Routing
LitePHP uses PHP 8's attributes for routing. The #[Route] attribute defines:
- The endpoint path
- The HTTP method
- Path parameters (using {parameter} syntax)
Gateways
Gateways provide an abstraction layer for database operations. Each gateway:
- Handles CRUD operations for a specific database table
- Uses PDO for secure database interactions
- Returns structured data to controllers
Gateway Example
Database
The database directory stores SQL migration files that can be executed with the db:migrate command. Example migration file:
Setting Up a Complete API Endpoint
1. Create the Database Table
Create an SQL file in the database directory:
Run the migration:
2. Create a Gateway
Then, update the generated gateway file:
3. Create a Controller
Then, update the generated controller:
4. Access Your API
With your server running (php lite start), you can now access your API:
Response Methods
The BaseController class provides several methods for returning standardized API responses:
respondNotFound($id): Returns a 404 error when a resource isn't foundrespondUnprocessableEntity($errors): Returns a 422 error for validation failuresrespondCreated($id): Returns a 201 status with the newly created resource's IDrespondInternalError($message): Returns a 500 error for server-side failures
Security Features
- Parameter Binding: All database queries use prepared statements with parameter binding to prevent SQL injection.
- JWT Authentication: Generate secure JWT keys with the
key:generatecommand for token-based authentication. - Input Validation: Each controller includes validation methods to ensure data integrity.
Future Development
- ORM Integration: An object-relational mapping system is planned for future releases to simplify database operations.
- Additional CLI commands for more development tasks.
- Extended middleware support.
Contributing
Contributions are welcome! Feel free to submit issues or pull requests to help improve LitePHP.
License
[MIT License]
Created By
Nihad Namatli
All versions of litephp with dependencies
ext-json Version *
ext-curl Version *
ext-openssl Version *
ext-pdo Version *
ext-pdo_mysql Version *
vlucas/phpdotenv Version ^5.6