Nano, by Hyperf
Nano is a zero-configuration, bare-bones, minimal Hyperf distribution that allows you to quickly build a Hyperf application from a SINGLE PHP file.
Design concept
Svelte’s authors claim that “frameworks don’t organize code, they organize ideas.” And one of the Nano’s biggest advantages is that it doesn’t interrupt your train of thought. The Nano is so good at claiming itself that it requires little understanding of the details of the framework and simply reads the code to know its purpose. Complete a complete Hyperf application through minimal code declaration.
features
- frameless
- Zero configuration
- Quick start
- Closure style
- Supports all Hyperf functions except annotation
- Compatible with all Hyperf components
- The Phar friendly
The installation
composer require hyperf/nanoCopy the code
Quick start
Create a PHP file such as index.php as follows:
<? 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();
Copy the code
Start the service:
php index.php startCopy the code
So simple.
More examples
routing
$APP integrates all methods of the Hyperf router.
<? 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();
Copy the code
DI container
<? 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();
Copy the code
$this is bound to Hyperf\Nano\ContainerProxy in all $app managed closure callbacks.
The middleware
<? 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();
Copy the code
In addition to closures, all $app->addXXX () methods also accept the class name as an argument. You can pass in the corresponding Hyperf class.
Exception handling
<? 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();
Copy the code
The command line
<? 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();
Copy the code
perform
php index.php echoCopy the code
Event listeners
<? 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();
Copy the code
Custom process
<? 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();
Copy the code
Timing task
<? 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();
Copy the code
Use more Hyperf components
<? 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();
Copy the code
To learn more, please visit:
Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.