Giter Site home page Giter Site logo

stephsalou / nano Goto Github PK

View Code? Open in Web Editor NEW

This project forked from midorikocak/nano

0.0 1.0 0.0 68 KB

Nano is a very very tiny php library that allows you to create very fast rest APIs. Think it's like Slim but if Slim is slim, Nano is anorexic.

License: MIT License

PHP 100.00%

nano's Introduction

nano API

Nano

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Nano is a very very tiny php library that allows you to create very fast rest APIs.

Think it's like Slim but if Slim is slim, Nano is anorexic.

Requirements

Strictly requires PHP 7.4.

Install

Via Composer

$ composer require midorikocak/nano

Usage

Simply instantiate and include in your app.

use midorikocak\nano\Api;

require __DIR__ . '/vendor/autoload.php';

$api = new Api();

I know. It's not static.

Defining REST resources

Defining rest routes and using wildcards are easy.

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

Basic Auth

It's possible hide your routed behind an authentication layer. Currently it expects basic auth, more methods to come soon.

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

Hence the basic auth is not encrypted, using https is strictly advised.

Testing

You can test your live API using Guzzle/Client

<?php

declare(strict_types=1);

namespace midorikocak\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());
    }

Motivation

Mostly educational purposes.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

nano's People

Contributors

midorikocak avatar

Watchers

James Cloos avatar

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.