Code Monkey home page Code Monkey logo

simple-chat-backend's Introduction

Simple PHP Chat Backend

  • Lumen micro-framework is used for this project.

  • All API codes are under app/Chat folder.

  • Output format is JSON but i created an interface to add more types in future.

  • I used Eloquent ORM of the framework but used Repository pattern to make easy to change it.

  • No any authentication system, so everybody can add a message using user ids.

  • There are some dummy data in SQLite db.

  • I wrote migrations to create DB tables but you need to add some user manually. You can use DB Browser for SQLite software.

  • You can create an empty file and use php artisan migrate command to generate the tables.

  • Routing codes are in routes/web.php file.

  • There are some unit tests on tests/MessageTest.php file

  • Created a Service Provider for Chat and added it to Laravel's bootstrap/app file to register it

  • Used Laravel's validation facade to validate input datas and improve security

#General Folder Structure

├───app
│ ├───Chat
│ │ └───Controllers
│ │ └───Models
│ │ └───Repositories
│ │ └───Views
│ ├───Http
│ │ └───Controllers
├───database
│ ├───migrations
├───routes
├───tests

Usage

Get unread messages of a user:

  • GET yourdomain/user/messages

  • Send a valid user_id (int)

  • It returns messages with user data

  • Example return message: [{"id":63,"user_id":"2","message":"Demo message","created_at":"2017-01-11 13:45:03","updated_at":"2017-01-11 13:45:03","is_read":"0","target_user_id":"3","user":{"id":2,"name":"Koray","created_at":2017-01-01,"updated_at":null},"target_user":{"id":3,"name":"John","created_at":2017-01-01,"updated_at":null}}]

Add a message to a user:

  • POST yourdomain/user/messages

  • Send a valid user_id (int), target_user_id (int) and message (string)

  • It updates the messages as read and returns them

  • Example return message: {"user_id":2,"message":"Test","target_user_id":3,"updated_at":"2017-01-11 13:53:18","created_at":"2017-01-11 13:53:18","id":64}

Installation

  • composer install
  • duplicate .env.example file and rename it as .env
  • open .env file
  • Set a app key that is random 32 character (not required)
  • Set DB_CONNECTION as sqlite
  • Set DB_DATABASE AS /YOUR-FULL-PATH/simple-chat-backend/database/db.sqlite
  • Change YOUR-FULL-PATH with your installation path

REQUIREMENTS

  • PHP >= 7.*
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension

TODO

  • Ordering options for get user messages API
  • Exceptions and error codes
  • Update a message
  • Delete a message
  • Add database seeds
  • Mocking for tests
  • Add more output types (HTML, XML, etc)
  • Authentication

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.