This project is a simple, "MVC-ish" RESTful API built with plain PHP, a SQLite database, and is fully containerized using Docker. It provides basic CRUD (Create, Read, Update) functionality for a Tasks resource.
.
├── app/
│ ├── controllers/
│ │ └── TaskController.php
│ ├── models/
│ │ └── TaskModel.php
│ ├── database.php
│ └── route.php
├── data/
│ └── database.sqlite # This is where the database file is stored
├── public/
│ └── index.php
├── .htaccess
├── Dockerfile
└── docker-compose.yml
-
public/: The entry point for the application. All requests are routed throughindex.php. -
.htaccess: Configures Apache to rewrite all requests topublic/index.php. -
app/: Contains the core logic of the API.-
route.php: The central router that maps incoming requests to the correct controller actions. -
database.php: Handles the SQLite database connection. -
controllers/TaskController.php: Contains the logic for handling task-related requests (GET,POST,PUT). -
models/TaskModel.php: Interacts directly with thetaskstable in the database.
-
-
data/: This directory is where thedatabase.sqlitefile will be created and stored. Docker volumes are used to ensure this data persists. -
Dockerfile: A script that contains instructions to build the Docker image for the application. -
docker-compose.yml: Defines the services (containers) needed to run the application, including port mapping and volumes.
-
Clone or download the project files.
git clone https://github.com/mikek1337/simple_php_api.git -
Navigate to the project's root directory in your terminal.
cd simple_php_api -
Run the following command to build the Docker image and start the container:
docker-compose up --buildThe
--buildflag is important the first time to create the image. Docker will download the necessary PHP image, configure the container, and start the Apache web server. -
Once the containers are up and running, the API will be accessible on your local machine at
http://localhost:8000.
You can interact with the API using a tool like curl or Postman. The Content-Type header for POST and PUT requests must be application/json.
-
Endpoint:
POST http://localhost:8000/tasks -
Example
curlcommand:curl -X POST http://localhost:8000/tasks -H 'Content-Type: application/json' -d '{"title": "Task 1", "description": "The first test task"}' -
Response (JSON):
{ "id": 1, "title": "Task 1", "description": "The first test task", "status": "pending", "created_at": "2020-08-03", "updated_at": "2020-08-"03 }
-
Endpoint:
GET http://localhost:8000/tasks -
Example
curlcommand:curl http://localhost:8000/tasks -
Response (JSON):
[ { "id": 1, "title": "Task 1", "description": "The first test task", "status": "pending", "created_at": "2020-08-03", "updated_at": "2020-08-"03 } ]
-
Endpoint:
GET http://localhost:8000/tasks/{id} -
Example
curlcommand (for task with ID 1):curl http://localhost:8000/tasks/1 -
Response (JSON):
{ "id": 1, "title": "Task 1", "description": "The first test task", "status": "pending", "created_at": "2020-08-03", "updated_at": "2020-08-"03 } -
Not Found Example: If the task ID does not exist, the response will be a
404 Not Foundwith an error message.
-
Endpoint:
PUT http://localhost:8000/tasks/{id} -
Example
curlcommand (for task with ID 1):curl -X PUT http://localhost:8000/tasks/1 \ -H "Content-Type: application/json" \ -d '{"status":"in-progress"}' -
Response (JSON):
{ "id": 1, "title": "Task 1", "description": "The first test task", "status": "in-progress", "created_at": "2020-08-03", "updated_at": "2020-08-"03 } -
Not Found Example: If the task ID does not exist, the response will be a
404 Not Found.
To stop the running Docker containers, press Ctrl+C in the terminal where docker-compose up is running.
To stop and remove the containers, networks, and the persistent data volume, run:
docker-compose down -v
Warning: The -v flag will delete the sqlite_data volume, which means all your task data in the database.sqlite file will be permanently lost. Use this command only when you want to start with a completely fresh database.