2017 © Pedro Peláez
 

library courser

light and faster php rest api lib

image

eclogue/courser

light and faster php rest api lib

  • Saturday, July 28, 2018
  • by superbogy
  • Repository
  • 1 Watchers
  • 2 Stars
  • 17 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Course

Build Status Coverage Status Latest Stable Version Total Downloads License, (*1)

some thing for fun, (*2)

Feature - PSR-15 middleware - PSR-7 http message - PSR-11 - coroutine support, (*3)

Installation

composer require eclogue/courser or git clone https://github.com/eclogue/courser, (*4)

Get start

composer install, (*5)

Simple usage php -S 127.0.0.1:7001 -t example/, (*6)

Router

<?php

# basic /users/11
$app->get('/users/:id', function($req, RequestHandlerInterface $handler) {
    var_dump($req->params['id']); // id must be integer
    return $handler->handle($req);
});
$app->get('/users/:id', function($req) {
    return ['data' => '1'];
});
# use array
$app->get('users/*', [function($req, RequestHandlerInterface $handler) {
    /* do something*/
}, function($req, RequestHandlerInterface $handler) {
    /*...todo*/
}]);

# use namespace
$app->put('/user/{username}', ['MyNamespace\Controller', 'action']);

# use group

$app->group('/admin/{username}',  function(App $app) {
    // [Notice]: In group `$this` is bind to Courser,
    // middleware define in group just have effect on the router of group scope 
    $app->used(function($req, RequestHandlerInterface $handler) { // Add group middleware
        // todo
        // this middleware is mount at /admin/{username} scope, have not effect outside of this group.
    });
    $app->get('/test/:id', function($req, RequestHandlerInterface $handler) {
        yield $handler->handle($req);
        // ...
    });
});

Middleware

Flow the PSR-15 standard, see https://github.com/middlewares/awesome-psr15-middlewares, (*7)

Not Found handle

$app->notFound(function (Request $req){
    $response = new Response();
    $response->withStatus(404)->json(['message' => 'Not Found']);
});

or, (*8)

# add after the last route
$app->add(new NotFoundMiddleware());

Exception

$app->setReporter(function ($req, $res, Exception $err) {
   $res->withStatus(500)->json([
       'message' =>$err->getMessage(),
       'code' => 10502,
   ]);
});

Coroutine

Courser support write coroutine application in easy way. support yield syntax. ``` // a middleware function process(Request $req, RequestHandlerInterface $handler) { $userId = $req->getParam('userId'); $model = new User(); $user = yield $model->findById($userId); var_dump($user); $response = yield $handler->handle($request);, (*9)

    return $response;
}

```, (*10)

Develop

Here is a tool quickly develop web app (gharry), (*11)

It watch project file change and auto reload your server., (*12)

Ben is a convenient config manager, I recommend use Ben to manage diff env config file., (*13)

Benchmark

Damn it. I just know that, it is fast.

Demo

Coding...

Maintain

mulberry10mulberry10th@gmail.com" title="See online mulberry10th@gmail.com">mulberry10th@gmail.com, (*14)

License

MIT

The Versions