some thing for fun
Feature
- PSR-15 middleware
- PSR-7 http message
- PSR-11
- coroutine support
composer require eclogue/courser
or git clone https://github.com/eclogue/courser
composer install
Simple usage php -S 127.0.0.1:7001 -t example/
<?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);
// ...
});
});
Flow the PSR-15 standard, see https://github.com/middlewares/awesome-psr15-middlewares
$app->notFound(function (Request $req){
$response = new Response();
$response->withStatus(404)->json(['message' => 'Not Found']);
});
or
# add after the last route
$app->add(new NotFoundMiddleware());
$app->setReporter(function ($req, $res, Exception $err) {
$res->withStatus(500)->json([
'message' =>$err->getMessage(),
'code' => 10502,
]);
});
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);
return $response;
}
Here is a tool quickly develop web app (gharry)
It watch project file change and auto reload your server.
Ben is a convenient config manager, I recommend use Ben to manage diff env config file.
Damn it. I just know that, it is fast.
mulberry10<mulberry10th@gmail.com>
MIT