项目作者: rjhdby

项目描述 :
Simple skeleton for php backend api
高级语言: PHP
项目地址: git://github.com/rjhdby/php-api-skeleton.git
创建时间: 2017-02-15T12:15:58Z
项目社区:https://github.com/rjhdby/php-api-skeleton

开源协议:MIT License

下载


Simple skeleton for php backend api

Code Climate
Build Status
Coverage Status
License: MIT

Project structure

  1. .
  2. ├── src
  3. ├── class
  4. ├── core # Classes that providing core functionality
  5. ├── Config.php # Providing methods for parse INI-files and retrieve his values
  6. ├── Controller.php # Main class that orchestrating api calls
  7. └── Method.php # Abstract class for 'methods' classes inheritance
  8. ├── methods # Classes that providing processing api calls
  9. ├── example
  10. └── Example.php # Example of simple api call handler
  11. └── ...
  12. └── errors
  13. └── ... # Custom exceptions
  14. └── db
  15. └── MyPdoConnection.php # Template singleton for PDO connection. Just for my own purposes. :)
  16. ├── config
  17. ├── environment.php # Global project properties
  18. ├── autoload.php # Autoload realization
  19. └── properties.php # User defined properties that can be retrieved by Config::get() method
  20. └── index.php # Entry point
  21. ├── test # PHPUnit test classes
  22. ├── bootstrap.php # Bootstrap for PHPUnit
  23. └── ...
  24. ├── LICENSE.txt # License. And what you expected?
  25. ├── README.md # This text
  26. └── ... # All other files is used for tests and code quality checks

Setup

PHP 5.5 and higher compatibility (tested on PHP 5.6+)

config/environment.php

File with global project properties. You must view and edit it before using this skeleton.

  1. define('ROOT', str_replace('\\', '/', __DIR__) . '/..'); //Root api directory
  2. define('PROPERTIES', ROOT . '/config/properties.php'); //Properties file path
  3. define('METHODS', '/tmp/methods.json'); //Temp file for methods cache
  4. define('DEBUG', true); //Whether use debug mode
  5. define('METHOD', 'm'); //Name of parameter in POST/GET data that contains method name
  6. define('GET', false); //Whether use $_GET instead of $_POST
  7. define('STATIC_MAPPING', false); //Whether use static class mapping
  8. define('CASE_SENSITIVE', true); //Whether api calls methods names is case sensitive

Using

See class/methods/Example.php.

  • Each method must extends abstract core\Method class.
  • All api-call classes should be placed under class/methods/ directory.
  • Method __construct($get, $post, $files, $body) will receive 4 associative arrays ($_GET, $_POST, $_FILES adn JSON-decoded request body)
  • Method __invoke must return an array or throw an Exception

The request to index.php must contains name of desired method, or an Wrong method error will be returned.

The response will be a JSON string.

Normal response

  1. {
  2. "r": ["an array returned by __invoke()"],
  3. "e": {}
  4. }

Error response

  1. {
  2. "r": {},
  3. "e": {
  4. "code": "$exception.getCode()",
  5. "text": "$exception.getMessage()"
  6. }
  7. }

Class Method

You can access to input data through those fields.

  1. $_GET['key'] === $this->get['key'] === $this['key'];
  2. $_POST['key'] === $this->post['key'];
  3. $_FILES['key'] === $this->files['key'];
  4. json_decode(file_get_contents('php://input'), true)['key'] === $this->body['key'];

methods

  1. protected function has(...$keys) // returns TRUE, if all keys present inside `$this->get`
  2. protected function missing($keys) // returns TRUE, if at least one key not present inside `$this->get`
  3. protected function checkParams(...$keys) // returns nothing. Throws ParameterException if at least one key not present inside `$this->get`

config/properties.php

Standard INI-file. All settings may be used inside the project with Config::get() method.

properties.php

  1. db_type=mysql
  2. db_host=localhost
  3. db_user=user
  4. db_pass=pass
  5. db_dbname=db

Your code

  1. $dbUser = core\Config::get('db_user');

Dynamic class mapping

  1. <?php
  2. namespace methods\core;
  3. use core\Method;
  4. class MyMethod extends Method {
  5. public function __invoke() {}
  6. }