Nano v1.0 發佈!一個 PHP 文件搭建 Hyperf 應用

Nano, by Hyperf

Nano 是一款零配置、無骨架、極小化的 Hyperf 發行版,經過 Nano 能夠讓您僅僅經過 1 個 PHP 文件便可快速搭建一個 Hyperf 應用。php

設計理念

Svelte 的做者提出過一個論斷:「框架不是用來組織代碼的,是用來組織思路的」。而 Nano 最突出的一個優勢就是不打斷你的思路。Nano 很是擅長於自我聲明,幾乎不須要瞭解框架細節,只須要簡單讀一讀代碼,就能知道代碼的目的。經過極簡的代碼聲明,完成一個完整的 Hyperf 應用。node

特性

  • 無骨架
  • 零配置
  • 快速啓動
  • 閉包風格
  • 支持註解外的所有 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();

 

啓動服務:架構

php index.php start

  

簡潔如此。app

 

更多示例

路由

$app 集成了 Hyperf 路由器的全部方法。composer

<?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();

  

執行ui

php index.php echo

  

事件監聽

<?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();

  

 

更多學習內容請訪問:this

騰訊T3-T4標準精品PHP架構師教程目錄大全,只要你看完保證薪資上升一個臺階(持續更新)

相關文章
相關標籤/搜索