|
一款零配置、无骨架、极小化的 Hyperf 发行版,通过 Nano 可以让您仅仅通过 1 个 PHP 文件即可快速搭建一个 Hyperf 应用
特性- 无骨架
- 零配置
- 快速启动
- 闭包风格
- 支持注解外的全部 Hyperf 功能
- 兼容全部 Hyperf 组件
- Phar 友好
安装
- composer require hyperf/nano
复制代码 快速开始创建一个 PHP 文件,如 index.php 如下: - <?php
- use Hyperf\Nano\Factory\AppFactory;
- require_once __DIR__ . '/vendor/autoload.php';
- $app = AppFactory::create();
- $app->get('/', function () {
- $user = $this->request->input('user', 'nano');
- $method = $this->request->getMethod();
- return [
- 'message' => "hello {$user}",
- 'method' => $method,
- ];
- });
- $app->run();
复制代码启动服务: 简洁如此。 更多示例路由$app 集成了 Hyperf 路由器的所有方法。 - <?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 容器
- <?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();
复制代码所有 app管理的闭包回调中,
this 都被绑定到了 Hyperf\Nano\ContainerProxy 上。 中间件
- <?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();
复制代码除了闭包之外,所有 $app->addXXX() 方法还接受类名作为参数。可以传入对应的 Hyperf 类。
异常处理- <?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();
复制代码
命令行
- <?php
- use Hyperf\Contract\StdoutLoggerInterface;
- use Hyperf\Nano\Factory\AppFactory;
- require_once __DIR__ . '/vendor/autoload.php';
- $app = AppFactory::create();
- $app->addCommand('echo', function(){
- $this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
- });
- $app->run();
复制代码 执行
事件监听
- <?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();
复制代码
自定义进程
- <?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->get(StdoutLoggerInterface::class)->info('Processing...');
- }
- });
- $app->run();
复制代码
定时任务
- <?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!');
- });
- $app->run();
复制代码
使用更多 Hyperf 组件
- <?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();
复制代码
游客,本帖隐藏的内容需要积分高于 2 才可浏览,您当前积分为 0
提取码下载:
|
|