Giter Club home page Giter Club logo

reactphp-rest-api's Introduction

ReactPHP-REST-API

This is a basic example for an api based on ReactPHP library.
It will show how to work with endpoints, and for this example we will use Games as a main item.

Dev env settings

After cloning / downloading this repository please follow the next steps:

  1. npm install - download and install all required npm packages
  2. composer install - download and install all packages of PHP

development npm packages

We will use a nodemon to run our servers with reloading on file change.
To start nodemon process just type npm run dev, it will start the server.php

Development CLI

After installing and downloading all the required packages from NPM and Composer you can use the rpc CLI tool to work with this project.
First, please run (in root project folder).

npm link

After running the npm link you can type
On windows

rpc --help

Or on Mac

./rpc.js --help

When running the command you should see the help section of the CLI.
If you have any problem running the CLI please contact me

How to work with this project

All the data you will see in this REST API data, is managed via Helpers classes.
The helpers can be found in the src folder under Helpers directory.
The only reason the helpers are here, is to include an example data without the need to integrate DB.
When you finish to develop your API please delete Helpers directory and files and use your real data.

Available Endpoints

For this example we will use:

  1. /games - GET
  2. /games - GET (with game id)
  3. /games - POST (with game id)
  4. /games - DELETE (with game id)
  5. /games - PUT (with game id)
  6. /games - OPTIONS
  7. /userbalance - GET (with user id)
  8. /userbalance - PUT (with user id)
  9. /userbalance - OPTIONS

Controller

You can build a controller however you want.
I decided to go on with the ReactPHP convention and to make an __invoke method.
For every endpoint, can have multiple controllers files, when every file will be take care for single method.
What it means is, that if I have endpoint with a get and post methods, I will have 2 controllers for this single endpoint.

HTTP Requests file

If you do not want to use postman or other tool to check and test this API and you are using PHP Storm,
You can use the requests.http and run any request you want to check.
Above of every request you will find a comment with a basic explanation of the request.

Router class

The router class will valid that the request is valid and if an endpoint is not found it will return the error response.
If the route is found and method is allowed it will dispatch the relevant endpoint

What is env.ini

This file should hold all environment configurations and need to be ignored from git.
Yes, i know that all the platforms using .env file and its cool and fun but i have work to do here and don't have time to find solutions.
To use env.ini data you can user the next function of php

parse_ini_file($_SERVER['DOCUMENT_ROOT'] . 'env.ini');

This will pass all the data from env.ini file parsed to array.
Good luck.

reactphp-rest-api's People

Stargazers

Hadj H. avatar

Watchers

James Cloos avatar

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.