PHP code example of maciejbuchert / nano

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

    

maciejbuchert / nano example snippets

 php
e maciejbuchert\nano\Api;

$api = new Api();

 php
$message = 'Welcome to Nano';

$api->get('/', function () use ($message) {
    echo json_encode(['message' => $message]);
    http_response_code(200);
});

$api->post('/', function () use ($message) {
    $input = (array)json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
    echo json_encode($input);
    http_response_code(201);
});

$api->get('/echo/{$message}', function ($message) {
    echo json_encode(['message' => $message]);
    http_response_code(200);
});

 php

$authFunction = function ($username, $password) {
    return ($username == 'username' && $password == 'password');
};

$api->auth(function () use (&$api) {

    $api->get('/entries/{id}', function ($id) {
        echo json_encode(['id' => $id]);
        http_response_code(201);
    });

    $api->post('/entries/{id}', function ($id) {
        echo json_encode(['id' => $id]);
        http_response_code(201);
    });

    $api->put('/entries/{id}', function ($id) {
        echo json_encode(['id' => $id]);
        http_response_code(204);
    });

    $api->delete('/entries/{id}', function ($id) {
        http_response_code(204);
    });

}, $authFunction);
 php


declare(strict_types=1);

namespace maciejbuchert\nano;

use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;

class IntegrationTest extends TestCase
{
    public function testGet(): void
    {
        $client = new Client(
            [
                'base_uri' => $this->baseUri,
                'http_errors' => false,
            ],
        );

        $response = $client->request('GET', '/echo/hello');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertStringContainsString('hello', (string)$response->getBody());
    }