This project is a simple CRUD API built using the Flight PHP framework. It provides a basic structure to quickly set up a PHP application with CRUD operations and database connectivity.
- Lightweight and fast setup with the Flight framework (v3.19).
- Basic CRUD (Create, Read, Update, Delete) operations for a
Userresource. - Routes grouped under
/usersand resolved lazily (Flight::group()+Class->methodstrings), so controllers are only instantiated when their route actually matches. - Consistent JSON responses, including for 404 (
Flight::map('notFound', ...)) and uncaught errors (Flight::map('error', ...)). - Configurable database connection via
.env. - Suitable for development with Apache, Nginx, or PHP's built-in server.
Before you begin, ensure you have the following installed on your machine:
- PHP 8.2 or higher
- A web server (Apache, Nginx, or PHP's built-in server)
- Composer (for dependency management)
- A MySQL or MariaDB database server
Follow these steps to set up the project:
-
Clone the Repository:
git clone https://github.com/soheilkhaledabdi/php-crud-api-flight.git cd php-crud-api-flight -
Install Dependencies:
Install the Flight framework and other dependencies using Composer:
composer install
-
Configure the Database:
Copy the .env.example file to .env:
cp .env.example .env
- Configure the Database: Update the .env file with your database credentials.
-
Run Database Migrations:
Use Phinx to run the database migrations:
vendor/bin/phinx migrate
-
Set Up a Web Server:
You can use Apache, Nginx, or PHP’s built-in server to run the project. Choose one of the following options:
-
Using PHP’s Built-In Server:
Navigate to the
publicdirectory and start the PHP server:cd public php -S localhost:8000Now, open your web browser and visit
http://localhost:8000. -
Using Apache or Nginx:
Set the document root to the
publicdirectory of the project. Configure the server to point topublic/index.php.For Apache, you might set up a virtual host like this:
<VirtualHost *:80> ServerName yoursite.local DocumentRoot /path/to/php-crud-api-flight/public <Directory /path/to/php-crud-api-flight/public> AllowOverride All Require all granted </Directory> </VirtualHost>
For Nginx, your server block might look like this:
server { listen 80; server_name yoursite.local; root /path/to/php-crud-api-flight/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
-
-
Testing the Application:
Once your server is set up, navigate to the URL configured (e.g.,
http://localhost:8000orhttp://yoursite.local) and test the CRUD functionality below.
All routes are grouped under /users and return JSON in the shape { "data": ..., "status": bool, "message": string }.
Example:
curl -X POST http://localhost:8000/users \
-H "Content-Type: application/json" \
-d '{"name": "Ada Lovelace", "email": "ada@example.com"}'The test suite uses Pest and requires a MySQL or MariaDB database.
-
Install the project dependencies:
composer install
-
Create a separate database for testing and configure its credentials in your
.envfile. -
Run the database migrations:
vendor/bin/phinx migrate
-
Run the test suite:
composer test
Important: Use a dedicated test database. The test suite clears the
userstable between tests to keep each test isolated.
We use the php-smart-validator package for validation. You can find more information and the source code at the following link:
https://github.com/geekGroveOfficial/php-smart-validator
If you wish to contribute to this project, please fork the repository, create a new branch for your feature or bug fix, and submit a pull request.
This project is open-source and available under the MIT License.
