2017 © Pedro PelĂĄez
 

library php-api

Create a API for your PHP project in minutes

image

ifroariquemes/php-api

Create a API for your PHP project in minutes

  • Tuesday, March 21, 2017
  • by natanaelsimoes
  • Repository
  • 2 Watchers
  • 1 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PhpAPI

Create a API for your PHP project in minutes. Simple add an annotation to yours methods and "voilĂĄ": it's done., (*1)

Usage

/**
 * @Api("my/route")
 * No @HttpMethod annotation, accepts all HTTP kinds of request methods
 */
public function myRoute() {
    // code
}

/**
 * @Api("route/with/$var")
 * @HttpMethod("PUT") // will accept only PUT requests
 */
public function routeWith($var) {
    // code
}

/**
 * @Api("another/$id/maybe/$var")
 * @HttpMethod("GET,POST") // will accept only GET and POST requests
 */
public function routeAnother($id, $var) {
    // code
}

As you can see, it uses a Symfony-like router to find the right code to execute. Moreover, it suports the use of patterns so some parts of request can be variables!, (*2)

Just add a$ inside @Api value to make it act as a method parameter., (*3)

Installation

This library can be found on Packagist. We endorse that everything will work fine if you install this through composer., (*4)

Add in your composer.json:, (*5)

{
    "require": {
        "ifroariquemes": "dev-master"
    }
}

or in your bash, (*6)

$ composer require ifroariquemes/php-api

From any point, make available the following code to start:, (*7)

require './vendor/autoload.php';
\PhpApi\PhpApi::start('src'); // your source code directory

If this script is available at http://example.com/api/v1/ (suposing you already have a router working previously) then the API router will understand things starting there: - http://example.com/api/v1/my/route - http://example.com/api/v1/route/with/me - http://example.com/api/v1/another/way/maybe/works, (*8)

If you are not using a router, but created a new directory to keep our code, remember to enable mod_rewrite and have a file like this its root to get things done:, (*9)

AddType application/x-httpd-php .php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
<Files "keys.json">
Order Allow,Deny
Deny from all
</Files>

This lib also uses a token system. You can activate it by creating a file named keys.json at API/project root folder:, (*10)

{
    "SystemName": "token123456"
}

The client will use this token to communicate with the API by adding its value to the key X-API-KEY within the HTTP request header., (*11)

Docs & Contribution

You can also check the docs/ for futher information about the classes and stuff. All codes are well commented so fell free to go deep and help us get this even better., (*12)

Profit!

The Versions