`hyperf/nano` is a package that scales Hyperf down to a single file. ## Example ```php get('/', function () { $user = $this->request->input('user', 'nano'); $method = $this->request->getMethod(); return [ 'message' => "hello {$user}", 'method' => $method, ]; }); $app->run(); ``` Run ```bash php index.php start ``` That's all you need. ## Feature * No skeleton. * Fast startup. * Zero config. * Closure style. * Support all Hyperf features except annotations. * Compatible with all Hyperf components. ## More Examples ### Routing $app inherits all methods from hyperf router. ```php 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 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 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 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 addCommand('echo', function(){ $this->get(StdoutLoggerInterface::class)->info('A new command called echo!'); }); $app->run(); ``` To run this command, execute ```bash php index.php echo ``` ### Event Listener ```php addListener(BootApplication::class, function($event){ $this->get(StdoutLoggerInterface::class)->info('App started'); }); $app->run(); ``` ### Custom Process ```php addProcess(function(){ while (true) { sleep(1); $this->get(StdoutLoggerInterface::class)->info('Processing...'); } }); $app->run(); ``` ### Crontab ```php addCrontab('* * * * * *', function(){ $this->get(StdoutLoggerInterface::class)->info('execute every second!'); }); $app->run(); ``` ### Use Hyperf Component. ```php 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(); ```