🚀 Build a Simple CRUD API in PHP with the Flight Framework

soheil-khaledabadi - Aug 23 - - Dev Community

Hello, fellow developers! 👋

If you're looking for a straightforward and lightweight PHP framework to build a RESTful API, look no further than Flight. Today, I'm excited to share a project I've been working on: a Simple CRUD API built using the Flight framework. This project serves as a boilerplate for anyone looking to quickly get started with building a CRUD application in PHP.

Why Use Flight?

Flight is a micro-framework for PHP that makes it easy to build web applications. It’s minimal, fast, and perfect for small to medium-sized projects where you want to avoid the overhead of a full-fledged framework like Laravel or Symfony. Here are some reasons why you might choose Flight for your next project:

  • Lightweight and Fast: With a small footprint, Flight provides just what you need to build your application without any bloat.
  • Easy to Learn: If you're familiar with PHP, you can quickly grasp Flight's syntax and start building right away.
  • Flexible: Flight allows you to structure your application as you see fit, providing freedom without enforcing a rigid structure.

About the Project

This project is a basic CRUD API that demonstrates how to use Flight for handling requests, connecting to a MySQL database, and performing CRUD operations (Create, Read, Update, Delete). It’s a great starting point for beginners or a quick setup for experienced developers.

Features

  • Basic CRUD Operations: Simple routes for creating, reading, updating, and deleting records in a MySQL database.
  • Database Configuration: Easily configurable database settings to connect to your own MySQL instance.
  • RESTful API Endpoints: Uses standard HTTP methods to interact with the API.
  • Minimal Setup: Get up and running with minimal configuration and dependencies.

Getting Started

To get started with the project, follow these steps:

1. Clone the Repository

First, clone the repository from GitHub:

git clone https://github.com/soheilkhaledabdi/php-crud-api-flight.git
cd php-crud-api-flight
Enter fullscreen mode Exit fullscreen mode

2. Install Dependencies

Install the Flight framework and any other dependencies using Composer:

composer install
Enter fullscreen mode Exit fullscreen mode

3. Configure Your Database

Set up your database connection in config/database.php:

return [
    'host' => 'your_database_host',
    'database' => 'your_database_name',
    'username' => 'your_database_username',
    'password' => 'your_database_password',
    'charset' => 'utf8mb4'
   ];
Enter fullscreen mode Exit fullscreen mode

4. Run the Project

You can use PHP’s built-in server to run the project:

cd public
php -S localhost:8000
Enter fullscreen mode Exit fullscreen mode

Then, open your browser and go to http://localhost:8000 to see the API in action.

How to Use the API

Once the server is running, you can interact with the API using tools like Postman or curl. Here’s a quick overview of the available endpoints:

  • Create a Record: Send a POST request to /create with JSON data.
  • Read Records: Send a GET request to /read to retrieve all records.
  • Update a Record: Send a PUT request to /update/{id} with JSON data.
  • Delete a Record: Send a DELETE request to /delete/{id}.

Contribute

If you have ideas to improve the project or find any bugs, feel free to open an issue or submit a pull request on GitHub. Contributions are always welcome!

Conclusion

This project is just a starting point, and there’s a lot more you can do with Flight. Whether you’re building a simple API or a more complex web application, Flight provides a flexible and lightweight solution.

I hope this project helps you get started with Flight and PHP. Happy coding! 🎉

. . . .