Need help with nano?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

hyperf
181 Stars 15 Forks MIT License 51 Commits 2 Opened issues

Description

🧬 Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.

Services available

!
?

Need anything else?

Contributors list

# 5,888
PHP
wechat
hyperf
Crystal
17 commits
# 23,295
PHP
hyperf
SQLite
postgre...
15 commits
# 5,961
PHP
Shell
hyperf
json-rp...
8 commits

English | 中文

Financial Contributors on Open Collective Php Version Swoole Version Nano License

Nano, by Hyperf

Nano is a zero-config, no skeleton, minimal Hyperf distribution that allows you to quickly build a Hyperf application with just a single PHP file.

Purpose

The author of

Svelte
has said that "Frameworks are not tools for organizing your code, they are tools for organizing your mind". The biggest advantages of Nano is that it doesn't interrupt your mind of thought. The Nano is good at self-declaration that you have no need to know the details of the framework. You can read the code fastly and know what it's for. Write a complete Hyperf application with minimal code declarations.

Feature

  • No skeleton.
  • Fast startup.
  • Zero config.
  • Closure style.
  • Support all Hyperf features except annotations.
  • Compatible with all Hyperf components.

Example

Create a single PHP file, like

index.php
:
require_once DIR . '/vendor/autoload.php';

$app = AppFactory::create('0.0.0.0', 9051);

$app->get('/', function () {

$user = $this->request->input('user', 'nano');
$method = $this->request->getMethod();

return [
    'message' => "hello {$user}",
    'method' => $method,
];

});

$app->run();

Run the server:

php index.php start

That's all you need.

More Examples

Routing

$app
inherits all methods from hyperf router.
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 Container

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

As a convention, $this is bind to ContainerProxy in all closures managed by nano, including middleware, exception handler and more.

Middleware

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

In addition to closure, all $app->addXXX() methods also accept class name as argument. You can pass any corresponding hyperf classes.

ExceptionHandler

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

Custom Command

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

To run this command, execute

bash
php index.php echo

Event Listener

require_once DIR . '/vendor/autoload.php';

$app = AppFactory::create();

$app->addListener(BootApplication::class, function($event){ $this->get(StdoutLoggerInterface::class)->info('App started'); });

$app->run();

Custom Process

require_once DIR . '/vendor/autoload.php';

$app = AppFactory::create();

$app->addProcess(function(){ while (true) { sleep(1); $this->get(StdoutLoggerInterface::class)->info('Processing...'); } });

$app->run();

Crontab

require_once DIR . '/vendor/autoload.php';

$app = AppFactory::create();

$app->addCrontab('* * * * * *', function(){ $this->get(StdoutLoggerInterface::class)->info('execute every second!'); });

$app->run();

Use Hyperf Component

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.