2017 © Pedro Peláez
 

project batio

A fast and extensible micro-framework for PHP to build RESTful API.

image

rocboss/batio

A fast and extensible micro-framework for PHP to build RESTful API.

  • Thursday, July 26, 2018
  • by rocboss
  • Repository
  • 1 Watchers
  • 4 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 83 % Grown

The README.md

Batio

Build Status Branch master Latest Stable Version Latest Unstable Version License, (*1)

English | 简体中文, (*2)

A fast and extensible micro-framework for PHP to build RESTful API., (*3)

1. Install

// (Recommend) If you’re using Composer, you can run the following command:
composer create-project --prefer-dist rocboss/batio batio
// Or git clone
git clone https://github.com/rocboss/batio.git batio
cd batio

cp .env.example .env
// Edit .env file
vim .env

composer install
chmod -R 755 app/storage

php -S 127.0.0.1:8888 -t public

Enter http://127.0.0.1:8888 in the browser's address bar. If everything is correct, you can get the following return:, (*4)

{
  "code": 0,
  "msg": "success",
  "data": "version: Batio 1.0.0"
}

Note: the initial installation needs to edit the related configuration information in the .env file under the project root, and you can also extend the other configuration in the file according to specific requirements., (*5)

2. Framework

2.1 Router

In app\config\routes.php, you can customize API routes., (*6)

route('GET /', ['api\HomeController', 'index']);

This is an ordinary route. When you visit the home page, you directly map to the api\HomeController controller, execute the following index method, and note that the type of controller method needs to be protected., (*7)

2.2 Middlewares

In app\config\app.php, you can customize Middleware for routes, such as authorization authentication, user roles control, etc., (*8)

// Middlewares
'middlewares' => [
    'auth' => AuthMiddleware::class,
],

Batio encapsulates a simple authentication model based on JWT, just call the auth() method after the routing of the authentication API., (*9)

route('GET /', ['api\HomeController', 'user'])->auth();

The example, (*10)

// Fail
{
    "code": 401,
    "msg": "[401 Unauthorized]."
}

// Success
{
    "code": 0,
    "msg": "success",
    "data": {
        "uid": 1,
        "user_name": "Jack",
        "user_age": 18
    }
}

When you send a request, pass the X-Authorization of JWT value to the server in header., (*11)

// This method can be used to obtain JWT.
\Auth::getToken($uid);

2.3 Cache

if (app()->cache('data')->contains('foo')) {
    $unit = app()->cache('data')->fetch('foo');
} else {
    $bar = 'bar cache';
    app()->cache('data')->save('foo', $bar);
}

2.4 Log

$logger = app()->log()->debug('debug log');

2.5 Database & Models

$userModel = new UserModel();
$userModel->name = 'Jack';
$userModel->email = 'bar@foo.com';
$userModel->avatar = 'https://foo.com/xxxxxx.png';
$userModel->password = password_hash("mypassword", PASSWORD_DEFAULT);
$userModel->save();

In app\models, model and service are stored, model is mainly dealing with database. The official recommended practice is that service calls model, controller calls service, so that the design makes the layering more reasonable, and the functional modules are decoupled to facilitate the business system., (*12)

Mainly depended on

lcobucci/jwt: 3.2.*
mikecao/flight: 1.3.*
aryelgois/medools: 5.0
catfan/medoo: 1.5.*
monolog/monolog: 1.23.*
doctrine/cache: 1.4.*
vlucas/phpdotenv: 2.0.*
predis/predis: 1.1.*
ruflin/elastica: 6.1.*
elasticsearch/elasticsearch: 6.0.*

Batio uses some excellent third party components, and you can get specific documents from their websites., (*13)

Authorization agreement

MIT Agreement, (*14)

The Versions

08/04 2018
08/04 2018
08/04 2018
02/11 2017
02/11 2017