paliari/nano-http

0.0.2 2020-05-02 21:58 UTC

This package is auto-updated.

Last update: 2024-08-29 05:49:45 UTC


README

最快的http php框架

如何安装

composer require paliari/nano-http

使用

<?php
require 'vendor/autoload.php';

路由

HTTP方法:GET、POST、PUT、PATCH、DELETE

映射

//...

$app = new \Paliari\NanoHttp\App();

$app->get('/', function ($req, \Paliari\NanoHttp\Http\Response $res) {
    $res->body = json_encode(['message' => 'Welcome']);

    return $res;
});
$app->post('/person', function ($req, \Paliari\NanoHttp\Http\Response $res) {
    $params = $req->post();
    $person = yourMethodCreatePerson($params);
    $res->body = json_encode($person);

    return $res;
});
$app->get('/person/{id}', function ($req, \Paliari\NanoHttp\Http\Response $res, $id) {
    $person = yourMethodGetPerson($id);
    $res->body = json_encode($person);

    return $res;
});

自定义映射

App->map(method: string, route: string, callable: string|callable) 如果callable是字符串,它必须用":"分隔,例如:"ClassName:metodName"。

$app->map($method, $route, $callable);

中间件

使用MiddlewareInterface

<?php

class AuthMiddleware implements \Paliari\NanoHttp\Middleware\MiddlewareInterface
{
    public function __invoke($request, $response, $next)
    {
        // TODO: Implement __invoke() method.
        // ...

        return $response;
    }
}
<?php

//...

$authMiddleware = new AuthMiddleware();
$aclMiddleware = new AclMiddleware();
$customMiddleware = new CustomMiddleware();
$app->add($customMiddleware, '/custom/path')
    ->add($aclMiddleware, '/')
    ->add($authMiddleware, '/')
;