Skip to content

An API Implementation in PHP without any Frameworks with DDD Design and Hexagonal Architecture

Notifications You must be signed in to change notification settings

adrianRoldan/api-without-frameworks

Repository files navigation

API without frameworks in PHP | DDD & Hexagonal Architecture

This project tries to be a native implementation of an API in PHP. It's decoupled from any framework and only uses PHP-DI as a dependency container.

Environment Setup

Needed tools

  1. Install Docker
  2. Install Composer
  3. Clone this project: git clone https://github.com/adrianRoldan/api-without-frameworks.git api-users-php
  4. Move to the project folder: cd api-users-php

Environment Configuration

  1. Run: cp config/database.example.php config/database.php
  2. Configure database.php file with:
"host"      => "database"
"user"      => "api"
"pass"      => "api"
"database"  => "api-users"
"port"      => 3306
  1. Run: composer install

UP Environment

  1. Execute docker-compose build
  2. Execute docker-compose up -d

API execution

The application runs in the port 3000: http://localhost:3000/

API Endpoints

The postman collection.json file has the collection of API requests available with the input data to import into postman.

Create user:

POST /user/create/

Input parameters:

{
    "name" : "name",
    "lastName" : "lastName",
    "phone" :  "phone"
}

Get User Contacts:

GET /user/contacts

Input parameters:

userId=userId

Create user contacts:

POST /user/contacts/create

Input parameters:

{
    "userId": 2,
    "contacts": [
        ...
        {
            "contactName": "Paco",
            "phone": 639916718
        },
        {
            "contactName": "Adela",
            "phone": 634716718
        },
        {
            "contactName": "Angel",
            "phone": 639916712
        }
        ...
    ]
}

Update user contacts:

PUT /user/contacts/update

Input parameters:

{
    "userId": 1,
    "contacts": [
        {
            "contactName": "Martin",
            "phone": 939916718
        },
        {
            "contactName": "Guillermo",
            "phone": 934512114
        }
    ]
}

Get shared Contacts:

GET /user/contacts/common

Input parameters:

userId1=userId1
userId2=userId2

Tests execution

  1. Execute PHPUnit tests: docker exec api-users-apache-php ./vendor/bin/phpunit

Project explanation

This is a simple users API. Users can have none or many contacts in their address book.

Conceptual diagram

About

An API Implementation in PHP without any Frameworks with DDD Design and Hexagonal Architecture

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published