hyperf/nano
Composer 安装命令:
composer create-project hyperf/nano
包简介
Scale Hyperf application down to a single file
README 文档
README
English | 中文
Nano, by Hyperf
Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.
Purpose
The author of Svelte has said that "Frameworks are not tools for organizing your code, they are tools for organizing your mind". The biggest advantages of Nano is that it doesn't interrupt your mind of thought. The Nano is good at self-declaration that you have no need to know the details of the framework. You can read the code fastly and know what it's for. Write a complete Hyperf application with minimal code declarations.
Feature
- No skeleton.
- Fast startup.
- Zero config.
- Closure style.
- Support all Hyperf features except annotations.
- Compatible with all Hyperf components.
Example
Create a single PHP file, like index.php:
<?php use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create('0.0.0.0', 9051); $app->get('/', function () { $user = $this->request->input('user', 'nano'); $method = $this->request->getMethod(); return [ 'message' => "hello {$user}", 'method' => $method, ]; }); $app->run();
Run the server:
php index.php start
That's all you need.
More Examples
Routing
$app inherits all methods from hyperf router.
<?php use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->addGroup('/nano', function () use ($app) { $app->addRoute(['GET', 'POST'], '/{id:\d+}', function($id) { return '/nano/'.$id; }); $app->put('/{name:.+}', function($name) { return '/nano/'.$name; }); }); $app->run();
DI Container
<?php use Hyperf\Nano\ContainerProxy; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; class Foo { public function bar() { return 'bar'; } } $app = AppFactory::create(); $app->getContainer()->set(Foo::class, new Foo()); $app->get('/', function () { /** @var ContainerProxy $this */ $foo = $this->get(Foo::class); return $foo->bar(); }); $app->run();
As a convention, $this is bind to ContainerProxy in all closures managed by nano, including middleware, exception handler and more.
Middleware
<?php use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->get('/', function () { return $this->request->getAttribute('key'); }); $app->addMiddleware(function ($request, $handler) { $request = $request->withAttribute('key', 'value'); return $handler->handle($request); }); $app->run();
In addition to closure, all $app->addXXX() methods also accept class name as argument. You can pass any corresponding hyperf classes.
ExceptionHandler
<?php use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->get('/', function () { throw new \Exception(); }); $app->addExceptionHandler(function ($throwable, $response) { return $response->withStatus('418') ->withBody(new SwooleStream('I\'m a teapot')); }); $app->run();
Custom Command
<?php use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->addCommand('echo {--name=Nano}', function($name){ $this->output->info("Hello, {$name}"); })->setDescription('The echo command.'); $app->run();
To run this command, execute
php index.php echo
Event Listener
<?php use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Framework\Event\BootApplication; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->addListener(BootApplication::class, function($event){ $this->get(StdoutLoggerInterface::class)->info('App started'); }); $app->run();
Custom Process
<?php use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->addProcess(function(){ while (true) { sleep(1); $this->container->get(StdoutLoggerInterface::class)->info('Processing...'); } })->setName('nano-process')->setNums(1); $app->addProcess(function(){ $this->container->get(StdoutLoggerInterface::class)->info('Determine whether the process needs to be started based on env...'); })->setName('nano-process')->setNums(1)->setEnable(\Hyperf\Support\env('PROCESS_ENABLE', true)))); $app->run();
Crontab
<?php use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->addCrontab('* * * * * *', function(){ $this->get(StdoutLoggerInterface::class)->info('execute every second!'); })->setName('nano-crontab')->setOnOneServer(true)->setMemo('Test crontab.'); $app->run();
Use Hyperf Component
<?php use Hyperf\DB\DB; use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->config([ 'db.default' => [ 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 3306), 'database' => env('DB_DATABASE', 'hyperf'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), ] ]); $app->get('/', function(){ return DB::query('SELECT * FROM `user` WHERE gender = ?;', [1]); }); $app->run();
How to use Swow
- require swow engine
composer require "hyperf/engine-swow:^2.0"
- run the code
<?php declare(strict_types=1); use Hyperf\Nano\Factory\AppFactory; require_once __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::createSwow(); $app->get('/', function () { return 'Hello World'; }); $app->run();
hyperf/nano 适用场景与选型建议
hyperf/nano 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.89k 次下载、GitHub Stars 达 434, 最近一次更新时间为 2020 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「nano」 「hyperf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hyperf/nano 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hyperf/nano 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hyperf/nano 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An async event for hyperf.
laylatichy/nano-modules-testing
php hyperf xxljob
SForum
Minimalistic and self-contained cURL HTTP REST client for PHP
统计信息
- 总下载量: 13.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 434
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-14