Simple REST API using Flight Framework + PHP-JWT + Idiorm
requires PHP 5.3
or greater
$ git clone https://github.com/davchezt/rest-api.git
$ cd rest-api
$ composer install
config file: config.php
'app' => [
'hash' => 'DaVchezt', // password hash
'debug' => false, // show debug request
'log' => true, // write log to file
'secret' => 'U0hiqmizT7repIgy3wX1uJv6R3T8YtskNcZmF7ClH2ajBtE4nF8WXQGNAw5b3VVe'
]
'db' => [
'dsn' => "mysql:host=localhost;dbname=dbname",
'dbu' => "dbuser",
'dbp' => "dbpass"
]
'mail' => [
'host' => 'mail.domain.com',
'user' => 'davchezt@domain.com',
'pass' => 'mailpass',
'name' => 'Leonardo DaVchezt'
]
Create new file and save to dir app/Router/
<?php
// file: app/Router/Main.php
namespace app\Router;
use app\BaseRouter;
class Main extends BaseRouter
{
public function init()
{
// call this with url http://localhost/rest-api/
$this->app->route('/', function () {
$this->app->json(['response' => ['data' => 'This is Main']]);
});
// this route is require authorize using JWT
$this->app->route('GET|POST /test', function () {
$this->app->json(['response' => ['data' => 'This is JWT test']]);
}, false, true); // set last argument with true to enable
}
}
?>
Create new file and save to dir app/Adapter/
<?php
// file: app/Adapter/UserAdapter.php
namespace app\Adapter;
use app\ModelInterface;
use app\SQL;
class UserAdapter implements ModelInterface
{
public function getById($id = 0) : array
{
return ['id' => 1, 'name' => 'admin'];
}
public function getAll() : array
{
return [
['id' => 1, 'name' => 'admin'],
['id' => 2, 'name' => 'admin2'],
['id' => 3, 'name' => 'admin3']
];
}
...
}
?>
Example on UserAdapter app/Adapter/UserAdapter.php
public function getById($id = 0) : array
{
$id = intval($this->id);
return $this->orm()
->select_many('user.id', 'user.username ...')
->find_one($id);
}
Example on Main router app/Router/Main.php
public function init()
{
// call this with url http://localhost/rest-api/
$this->app->route('/', function () {
// initialize model
$user = new app\Adapter\UserAdapter();
// assign model
$this->app->model()->setAdapter($user);
// call model
$users = $this->app->model()->getAll();
$response = [
'users' => $users,
'count' => count($users)
];
// output
$this->app->json(['response' => $response]);
});
}