PHP code example of tsoffereins / nano

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

    

tsoffereins / nano example snippets


$container = new Container();

$container->bind('abstract', 'concrete');

$instance = $container->make('abstract');

$container->bind('abstract', function()
{
    return new Concrete('config');
});

$container->singleton('abstract', function()
{
    return new Concrete('config');
});

$pipe = $container->make('Nano\PipeLine');

$pipe->addMiddleare(['Middleware']);

$pipe->fire(function()
{
    return 'Hello world!';
}, $request);

class Middleware
{
    public function handle($request, $next)
    {
        // Do something with request
        
        $response = $next($request);
        
        // Do something with response
        
        return $response;
    }
}

$router = $container->make('Nano\Router');

$router->addRoutes(['/home', 'Controller@index']);

echo $router->match($_SERVER['REQUEST_URI']);

class Controller
{
    public function index()
    {
        return 'Hello world!';
    }
}

$router->addRoutes(['/user/:id', 'Controller@user']);

$router->addRoutes([
    '/home', 'Controller@index', // defaults to GET
    'POST=/user/',
    'GET=/user/:id',
    'PUT=/user/:id',
    'DELETE=/user/:id'
]);