Giter Club home page Giter Club logo

fhp-rest-api's Introduction

FHP REST API Build Status (Master Build Status)

Description

FHP REST API provides an easy way to setup a JSON FILE based REST API.

There are no dependencies to MySQL or other databases. All files will be stored as json files by default. Still you may also integrate your own way of saving data, by integrating your own controllers, entites and normalizers.

I'm trying really hard to make this very flexible and at the same easy to understand, i hope you enjoy working with this.

Installation

$ composer require rozbehsharahi/fhp-rest-api

How to

The following index.php is enough to provide PUT, GET, POST, OPTION, DELETE routes for your application on the /posts route:

<?php
use Fhp\Rest\Api;
use Fhp\Rest\Repository\JsonRepository;
use Fhp\Rest\Controller\FlexEntityController;

require_once('vendor/autoload.php');

// Set json database folder
JsonRepository::setDirectory(__DIR__ . '/database/');

// Api call here
Api::create()
    ->activateEntity('post', FlexEntityController::class) // <-- use singular entity name
    ->run();

That's it!

How to use entity classes

(Still experimental)

This way of using FHP REST API is still very experimental, so please used this with caution. For instance you have to define fhp property type annotations, though they don't have any impact on the way a property is saved yet.

<?php
use Fhp\Rest\Api;
use Fhp\Rest\Repository\JsonRepository;

require_once('vendor/autoload.php');

// Set json database folder
JsonRepository::setDirectory(__DIR__ . '/database/');

// Api call here
Api::create()
    ->activateEntity(My\Example\Page::class)
    ->run();

Your entity class should look like this:

<?php
namespace My\Example;

/**
 * My Entity class
 *
 * @class Page
 */
class Page
{

    /**
     * @Fhp\Rest\PropertyType\StringType
     */
    protected $id;

    /**
     * @Fhp\Rest\PropertyType\StringType
     */
    protected $url;

    public function getId() { return $this->id; }

    public function setId($id) { $this->id = $id; return $this; }

    public function getUrl() { return $this->url; }

    public function setUrl($url) { $this->url = $url; return $this; }
}

At the moment it does not make any difference which property type you will use. But it is a good preparation for next releases. You will still have to annotate with one of the following annotations:

  • @Fhp\Rest\PropertyType\StringType
  • @Fhp\Rest\PropertyType\BooleanType
  • @Fhp\Rest\PropertyType\IntegerType

Defining custom entity-controllers

You may also define your own controllers to handle model-requests. Just pass your controller's class name as the third parameter to $api->createModel.

Please extend Fhp\Rest\Controller and feel free to override the basic actions:

  • public function indexAction($request, $response, $args) {}
  • public function showAction($request, $response, $args) {}
  • public function updateAction($request, $response, $args) {}
  • public function deleteAction($request, $response, $args) {}
  • public function createAction($request, $response, $args) {}

by calling $api->activateEntity like following:

$api->activateEntity(
    \My\Entity\Post::class,
    \My\Own\PostController::class
);

Super fast start without Apache

No apache, No MySQL, just make sure PHP is installed on your machine.

Since FHP REST API is super independent you may start your server on almost any machine that supports PHP7+ (in future also PHP5.6+). Not even Apache is needed to get it started. Just change your directory to the projects root and go on with:

$ php -S localhost:8000

Start with Apache

In case you want to start FHP REST API on an apache server, please add the following .htaccess file to your project's root where index.php is located.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Security issues / CORS

FHP REST API has a set of default-headers that are defined in Fhp\Rest\Api. These headers also contain 'Access-Control-Allow-Origin' => '*' which is a security issue depending on the project.

Please make sure to configure your own header-settings when going to production.

You may do this by following:

$api->setHeaders([
    'Content-Type' => 'application/json',
    'Access-Control-Allow-Origin' => 'http://my-specific-doain.com'
]);

Todos and issues

  • PropertyTypes
  • Refactoring

fhp-rest-api's People

Contributors

rozbehsharahi avatar

Watchers

James Cloos avatar  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.