Skip to content

mikek1337/simple_php_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple PHP MVC API with SQLite and Docker

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.

Project Structure

.
├── 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 through index.php.

  • .htaccess: Configures Apache to rewrite all requests to public/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 the tasks table in the database.

  • data/: This directory is where the database.sqlite file 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.

Setup

  1. Clone or download the project files.

    git clone https://github.com/mikek1337/simple_php_api.git
    
  2. Navigate to the project's root directory in your terminal.

    cd simple_php_api
    
  3. Run the following command to build the Docker image and start the container:

    docker-compose up --build
    

    The --build flag 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.

  4. Once the containers are up and running, the API will be accessible on your local machine at http://localhost:8000.

API Endpoints and Usage

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.

1. Create a New Task (POST)

  • Endpoint: POST http://localhost:8000/tasks

  • Example curl command:

        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
    }
    

2. Retrieve All Tasks (GET)

  • Endpoint: GET http://localhost:8000/tasks

  • Example curl command:

    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
    }
    ]
    

3. Retrieve a Single Task (GET)

  • Endpoint: GET http://localhost:8000/tasks/{id}

  • Example curl command (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 Found with an error message.

4. Update a Task (PUT)

  • Endpoint: PUT http://localhost:8000/tasks/{id}

  • Example curl command (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.

Stopping the Containers

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published