PHPackages                             midorikocak/nano - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. midorikocak/nano

ActiveLibrary[HTTP &amp; Networking](/categories/http)

midorikocak/nano
================

Nano is a very very tiny php app that allows you to create very fast rest interfaces.

v1.3.1(6y ago)83417MITPHPPHP ~7.4

Since Jan 24Pushed 5y ago6 watchersCompare

[ Source](https://github.com/midorikocak/nano)[ Packagist](https://packagist.org/packages/midorikocak/nano)[ Docs](https://github.com/midorikocak/nano)[ RSS](/packages/midorikocak-nano/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

[![nano API](nano.png)](nano.png)

Nano
====

[](#nano)

[![Latest Version on Packagist](https://camo.githubusercontent.com/05180841d2a007a3bf1f559ecab0368bb247e096bcf6dd744e349121d6e69334/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d69646f72696b6f63616b2f6e616e6f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/midorikocak/nano)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d96e2b4951b249b2f16a15df40509ef727384e39740ef71ff0a9096bda291fd2/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d69646f72696b6f63616b2f6e616e6f2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/midorikocak/nano)[![Coverage Status](https://camo.githubusercontent.com/e09ce973f1da4d127a1b397a4c2184c2541d639a698b607efc85b271d164fb4b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d69646f72696b6f63616b2f6e616e6f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/midorikocak/nano/code-structure)[![Quality Score](https://camo.githubusercontent.com/3213f17575e6eaf35e1d92722d75be2a55a8b290e6698273fe0464a2a8cec3f7/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d69646f72696b6f63616b2f6e616e6f2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/midorikocak/nano)[![Total Downloads](https://camo.githubusercontent.com/db85732c0f4232f5af484fa8bc557bacce7c1a6ed6c6b08e4d390f2a155045d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d69646f72696b6f63616b2f6e616e6f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/midorikocak/nano)

**CONTRIBUTORS WELCOME**

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

Think it's like Slim but Nano is only **~6.4 Kilobytes**.

Requirements
------------

[](#requirements)

Strictly requires PHP 7.4.

Install
-------

[](#install)

Via Composer

```
$ composer require midorikocak/nano
```

Usage
-----

[](#usage)

Simply instantiate and include in your app.

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

use midorikocak\nano\Api;

$api = new Api();
```

I know. It's not static.

### Defining REST resources

[](#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

[](#basic-auth)

It's possible hide your routes 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
-------

[](#testing)

You can test your live API using `Guzzle/Client`

```
