Giter Site home page Giter Site logo

courser's Introduction

Course

Build Status Coverage Status Latest Stable Version Total Downloads License

some thing for fun

Feature

  • PSR-15 middleware
  • PSR-7 http message
  • PSR-11
  • coroutine support

Installation

composer require eclogue/courser or git clone https://github.com/eclogue/courser

Get start

composer install

Simple usage php -S 127.0.0.1:7001 -t example/

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

Not Found handle

$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());

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);

      return $response;
  }

Develop

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.

Benchmark

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

Demo

Coding...

Maintain

mulberry10<[email protected]>

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.