一个用于简单应用的最小化尺寸框架。

v2.0.0 2021-05-11 17:48 UTC

This package is auto-updated.

Last update: 2024-09-12 01:17:04 UTC


README

Nano 是一个非常小巧的框架,它提供了创建最小化应用的必要功能,例如,创建一个非常瘦小且超级快速的 API。

安装

可以使用 Composer 下载和安装 Nano 框架及其依赖项。

composer require nano/framework

构建块

Nano 框架包含以下类

  • Nano\Container - 一个用于依赖注入的 IoC 容器
  • Nano\PipeLine - 一个中间件总线
  • Nano\Router - 一个用于定义端点的路由器

容器

使用容器

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

支持

请在此处 GitHub 上提交问题