通过 hyperf/nano 可以在无骨架、零配置的情况下快速搭建 Hyperf 应用。

安装

  1. composer install hyperf/nano

快速开始

  1. <?php
  2. // index.php
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create('0.0.0.0', 9051);
  6. $app->get('/', function () {
  7. $user = $this->request->input('user', 'nano');
  8. $method = $this->request->getMethod();
  9. return [
  10. 'message' => "hello {$user}",
  11. 'method' => $method,
  12. ];
  13. });
  14. $app->run();

启动:

  1. php index.php start

简洁如此。

特性

  • 无骨架
  • 零配置
  • 快速启动
  • 闭包风格
  • 支持注解外的全部 Hyperf 功能
  • 兼容全部 Hyperf 组件
  • Phar 友好

更多示例

路由

$app 集成了 Hyperf 路由器的所有方法。

  1. <?php
  2. use Hyperf\Nano\Factory\AppFactory;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. $app = AppFactory::create();
  5. $app->addGroup('/nano', function () use ($app) {
  6. $app->addRoute(['GET', 'POST'], '/{id:\d+}', function($id) {
  7. return '/nano/'.$id;
  8. });
  9. $app->put('/{name:.+}', function($name) {
  10. return '/nano/'.$name;
  11. });
  12. });
  13. $app->run();

DI 容器

  1. <?php
  2. use Hyperf\Nano\ContainerProxy;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. class Foo {
  6. public function bar() {
  7. return 'bar';
  8. }
  9. }
  10. $app = AppFactory::create();
  11. $app->getContainer()->set(Foo::class, new Foo());
  12. $app->get('/', function () {
  13. /** @var ContainerProxy $this */
  14. $foo = $this->get(Foo::class);
  15. return $foo->bar();
  16. });
  17. $app->run();

所有 $app 管理的闭包回调中,$this 都被绑定到了 Hyperf\Nano\ContainerProxy 上。

中间件

  1. <?php
  2. use Hyperf\Nano\Factory\AppFactory;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. $app = AppFactory::create();
  5. $app->get('/', function () {
  6. return $this->request->getAttribute('key');
  7. });
  8. $app->addMiddleware(function ($request, $handler) {
  9. $request = $request->withAttribute('key', 'value');
  10. return $handler->handle($request);
  11. });
  12. $app->run();

除了闭包之外,所有 $app->addXXX() 方法还接受类名作为参数。可以传入对应的 Hyperf 类。

异常处理

  1. <?php
  2. use Hyperf\HttpMessage\Stream\SwooleStream;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create();
  6. $app->get('/', function () {
  7. throw new \Exception();
  8. });
  9. $app->addExceptionHandler(function ($throwable, $response) {
  10. return $response->withStatus('418')
  11. ->withBody(new SwooleStream('I\'m a teapot'));
  12. });
  13. $app->run();

命令行

  1. <?php
  2. use Hyperf\Contract\StdoutLoggerInterface;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create();
  6. $app->addCommand('echo', function(){
  7. $this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
  8. });
  9. $app->run();

执行

  1. php index.php echo

事件监听

  1. <?php
  2. use Hyperf\Contract\StdoutLoggerInterface;
  3. use Hyperf\Framework\Event\BootApplication;
  4. use Hyperf\Nano\Factory\AppFactory;
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. $app = AppFactory::create();
  7. $app->addListener(BootApplication::class, function($event){
  8. $this->get(StdoutLoggerInterface::class)->info('App started');
  9. });
  10. $app->run();

自定义进程

  1. <?php
  2. use Hyperf\Contract\StdoutLoggerInterface;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create();
  6. $app->addProcess(function(){
  7. while (true) {
  8. sleep(1);
  9. $this->get(StdoutLoggerInterface::class)->info('Processing...');
  10. }
  11. });
  12. $app->run();

定时任务

  1. <?php
  2. use Hyperf\Contract\StdoutLoggerInterface;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create();
  6. $app->addCrontab('* * * * * *', function(){
  7. $this->get(StdoutLoggerInterface::class)->info('execute every second!');
  8. });
  9. $app->run();

使用 Hyperf 组件.

  1. <?php
  2. use Hyperf\DB\DB;
  3. use Hyperf\Nano\Factory\AppFactory;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $app = AppFactory::create();
  6. $app->config([
  7. 'db.default' => [
  8. 'host' => env('DB_HOST', 'localhost'),
  9. 'port' => env('DB_PORT', 3306),
  10. 'database' => env('DB_DATABASE', 'hyperf'),
  11. 'username' => env('DB_USERNAME', 'root'),
  12. 'password' => env('DB_PASSWORD', ''),
  13. ]
  14. ]);
  15. $app->get('/', function(){
  16. return DB::query('SELECT * FROM `user` WHERE gender = ?;', [1]);
  17. });
  18. $app->run();