1. Go to this page and download the library: Download monomelodies/monki library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
monomelodies / monki example snippets
use Monki\Api;
$monki = new Monki('/base/url/of/api/');
$pipeline = (new Pipeline)
->pipe($monki);
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\ServerRequestFactory;
$response = $monki(ServerRequestFactory::fromGlobals());
if ($response instanceof ResponseInterface) {
// Emit the response
} else {
// No an API URL; use e.g. your router to determine further handling.
}
use Monomelodies\Monki\Handler\Crud;
class MyHandler extends Crud
{
}
$monki->crud('/api/user/', new MyHandler);
use Monomelodies\Monki\Handler\Crud;
class MyHandler extends Crud
{
public function browse()
{
// This should e.g. do a database query in real life:
$users = ['Marijn', 'Linus', 'Bill'];
return $this->jsonResponse($users);
}
}
use Quibble\Dabble\Adapter;
use Zend\Diactoros\Response\EmptyResponse;
class MyHandler extends Crud implements Browse, Create, Retrieve, Update, Delete
{
private $db;
private $table;
public function __construct(Adapter $db, $table)
{
$this->db = $db;
$this->table = $table;
}
public function browse($id = null)
{
$items = $this->db->selectFrom($this->table)
->fetchAll(PDO::FETCH_ASSOC);
return $this->jsonResponse($items);
}
public function create($id = null)
{
$this->db->insertInto($this->table)
->execute($_POST);
$id = $this->db->lastInsertId($this->table);
return $this->retrieve($id, 201);
}
public function retrieve($id, $status = 200)
{
$item = $this->db->selectFrom($this->table)
->where('id = ?', $id)
->fetch(PDO::FETCH_ASSOC);
return $this->jsonResponse($item, $status);
}
public function update($id)
{
$this->db->updateTable($this->table)
->where('id = ?', $id)
->execute($_POST);
return $this->retrieve($id);
}
public function delete($id)
{
$this->db->deleteFrom($this->table)
->where('id = ?', $id)
->execute();
return $this->emptyResponse(204);
}
}
// Assuming $user contains the currently logged in user...
$monki->crud('/api/user/', '/:id/', new MyHandler($db, 'user'))
->pipe(function ($payload) use ($user) {
if ($user->name != 'Marijn') {
// Bad user! No access.
return new EmptyResponse(403);
}
return $payload;
});
$check = function ($payload) use ($user) {
if ($user->name != 'Marijn') {
// Bad user! No access.
return new EmptyResponse(403);
}
return $payload;
};
$monki->crud('/api/user/', '/:id/', new MyHandler($db, 'user'))->pipe($check);
$monki->crud('/api/message/', '/:id/', new MyHandler($db, 'message'))->pipe($check);
$monki->crud('/api/foo/', '/:id/', new MyHandler($db, 'foo'))->pipe($check);
$monki->crud('/api/bar/', '/:id/', new MyHandler($db, 'bar'))->pipe($check);
// This endpoint is open for the world (usually a bad idea ;)):
$monki->crud('/api/baz/', '/:id/', new MyHandler($db, 'baz'));
// ...
class MyHandler extends Crud
{
/**
* @Method GET
* @Url /count/
*/
public function countIt()
{
return $this->jsonResponse(3);
}
}
class MyHandler extends Crud
{
/**
* @Method PUT
* @Url /custom/:id/
*/
public function thisIsSomethingCustom($id)
{
// ...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.