https://github.com/rocboss/batio
A fast and extensible micro-framework for PHP to build RESTful API.
https://github.com/rocboss/batio
batio batio-framework php restful-api
Last synced: 12 months ago
JSON representation
A fast and extensible micro-framework for PHP to build RESTful API.
- Host: GitHub
- URL: https://github.com/rocboss/batio
- Owner: rocboss
- License: mit
- Created: 2017-10-28T16:54:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-04T07:31:38.000Z (over 7 years ago)
- Last Synced: 2025-03-27T01:47:50.309Z (about 1 year ago)
- Topics: batio, batio-framework, php, restful-api
- Language: PHP
- Homepage:
- Size: 50.8 KB
- Stars: 90
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Batio
[](https://www.travis-ci.org/rocboss/batio)

[](https://packagist.org/packages/rocboss/batio)
[](https://packagist.org/packages/rocboss/batio)
[](https://packagist.org/packages/rocboss/batio)
English | [简体中文](./README_zh-CN.md)
> A fast and extensible micro-framework for PHP to build RESTful API.
## 1. Install
```bash
// (Recommend) If you’re using Composer, you can run the following command:
composer create-project --prefer-dist rocboss/batio batio
```
```bash
// Or git clone
git clone https://github.com/rocboss/batio.git batio
```
```bash
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:
```json
{
"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.
## 2. Framework
### 2.1 Router
In `app\config\routes.php`, you can customize API routes.
```php
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`.
### 2.2 Middlewares
In `app\config\app.php`, you can customize `Middleware` for routes, such as authorization authentication, user roles control, etc.
```php
// 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.
```php
route('GET /', ['api\HomeController', 'user'])->auth();
```
The example
```json
// 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`.
```php
// This method can be used to obtain JWT.
\Auth::getToken($uid);
```
### 2.3 Cache
```php
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
```php
$logger = app()->log()->debug('debug log');
```
### 2.5 Database & Models
```php
$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.
### 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.
## Authorization agreement
[MIT Agreement](http://opensource.org/licenses/MIT)