赞
踩
Nano 是一款零配置、无骨架、极小化的 Hyperf 发行版,通过 Nano 可以让您仅仅通过 1 个 PHP 文件即可快速搭建一个 Hyperf 应用。
Svelte
的作者提出过一个论断:“框架不是用来组织代码的,是用来组织思路的”。而 Nano 最突出的一个优点就是不打断你的思路。Nano 非常擅长于自我声明,几乎不需要了解框架细节,只需要简单读一读代码,就能知道代码的目的。通过极简的代码声明,完成一个完整的 Hyperf 应用。
composer require hyperf/nano
创建一个 PHP 文件,如 index.php 如下:
- <?php
- use HyperfNanoFactoryAppFactory;
-
- 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();
启动服务:
php index.php start
简洁如此。
$app
集成了 Hyperf 路由器的所有方法。
- <?php
- use HyperfNanoFactoryAppFactory;
-
- 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();
- <?php
- use HyperfNanoContainerProxy;
- use HyperfNanoFactoryAppFactory;
- 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 都被绑定到了 HyperfNanoContainerProxy
上。
- <?php
- use HyperfNanoFactoryAppFactory;
-
- 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 HyperfHttpMessageStreamSwooleStream;
- use HyperfNanoFactoryAppFactory;
-
- 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 HyperfContractStdoutLoggerInterface;
- use HyperfNanoFactoryAppFactory;
-
- 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 index.php echo
- <?php
- use HyperfContractStdoutLoggerInterface;
- use HyperfFrameworkEventBootApplication;
- use HyperfNanoFactoryAppFactory;
-
- 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 HyperfContractStdoutLoggerInterface;
- use HyperfNanoFactoryAppFactory;
-
- 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 HyperfContractStdoutLoggerInterface;
- use HyperfNanoFactoryAppFactory;
-
- require_once __DIR__ . '/vendor/autoload.php';
- $app = AppFactory::create();
-
- $app->addCrontab('* * * * * *', function(){
- $this->get(StdoutLoggerInterface::class)->info('execute every second!');
- });
-
- $app->run();
- <?php
- use HyperfDBDB;
- use HyperfNanoFactoryAppFactory;
-
- 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();
-
原文;Nano v1.0 发布!一个 PHP 文件搭建 Hyperf 应用
更多学习内容请访问:
八重樱:腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)zhuanlan.zhihu.com以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的官方群点击此处。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。