PHP code example of nash / pin

1. Go to this page and download the library: Download nash/pin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nash / pin example snippets



ction () {
    App::get('/', fn()=>'Hello World Too !!!');
})();



tion () {
    App::group('/api', function () {
        App::route(['GET', 'POST'], '/a', fn()=>['id' => 1]);
        App::get('/b', fn()=>['id' => 2]);
        App::post('/c', fn()=>['id' => 3]);
        App::put('/d', fn()=>['id' => 4]);
    });
})();



tion () {
    App::addMiddleware(fn(callable $next) => $next());
    App::get('/', fn()=>['id' => 1], fn(callable $next)=>$next(), fn(callable $next)=>$next());
})();



tion () {
    App::get('/', function (\Nash\Pin\Server\Request $request) {
        $request = \Nash\Pin\Server\Request::instance();
        // or
        $request = \Nash\Pin\Core\Container::instance()->get(\Nash\Pin\Server\Request::class);
    });
})();



tion () {
    App::command('demo:hello')
        ->setDescription('hello 命令')
        ->addArgument('name', \Nash\Pin\Command\Input::ARGUMENT_OPTIONAL, '名称', 'World')
        ->setCallback(function () {
            \Nash\Pin\Command\Output::instance()->writeln('Hello ' . \Nash\Pin\Command\Input::instance()->getArgument('name') . '!');
        });
})();



tion () {
    App::addExceptionHandler(function (Throwable $throwable) {
        App::printThrowable($throwable);
    });
})();



tion () {

    class MyEvent { public function __construct(public string $name) {} }

    App::listener(MyEvent::class)->setCallback(function (MyEvent $event) {
        var_dump($event);
    });

    App::command('demo')->setCallback(function () {
        \Nash\Pin\Listener\EventDispatcher::instance()->dispatch(new MyEvent('test'));
    });
})();



tion () {
    App::process('test')->setCallback(function () {
        while (true) {
            var_dump(date('Y-m-d H:i:s'));
            sleep(1);
        }
    });
})();



tion () {
    App::crontab('test')->setRule('* * * * *')->setCallback(function () {
        var_dump(date('Y-m-d H:i:s'));
    });
})();



tion () {
    App::setMode(\Nash\Pin\Core\Application::MODE_COROUTINE);
})();



tion () {
    // TCP
    App::setType(\Nash\Pin\Core\Server::TYPE_TCP);
    // UDP
    App::setType(\Nash\Pin\Core\Server::TYPE_UDP);
    // WEBSOCKET
    App::setType(\Nash\Pin\Core\Server::TYPE_WEBSOCKET);
})();



tion () {
    App::server('default')->get('/', fn() => 'default server');
    App::server('api')->setPort(9502)->get('/', fn() => 'api server');
})();
bash
php -d "swoole.use_shortname='Off'" -r "
bash
php -d "swoole.use_shortname='Off'" index.php start