PHP code example of paliari / nano-http

1. Go to this page and download the library: Download paliari/nano-http 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/ */

    

paliari / nano-http example snippets





//...

$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, $route, $callable);




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

        return $response;
    }
}




//...

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