1. Go to this page and download the library: Download fqapps/swoole-gin 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/ */
fqapps / swoole-gin example snippets
declare(strict_types=1);
use SwooleGin\Gin\Context\Context;
use SwooleGin\Gin\Context\ContextHandlerFuncInterface;
use SwooleGin\Gin\Gin;
use SwooleGin\Gin\Middleware\FaviconMiddleware;
use SwooleGin\Options;
use SwooleGin\Server;
use SwooleGin\Stream\StringStream;
use SwooleGin\Utils\HTTPStatus;
{
if ($context->query('token') !== '123456') {
$context->response->withBody(new StringStream('authorized failed'));
$context->response->withStatus(HTTPStatus::StatusForbidden);
$context->abort();
}
}
}),
// 用于模拟洋葱结构中间件:打印响应内容
(new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->next();
$body = $context->response->getBody()->getContents();
echo 'resp:', $body, PHP_EOL;
$context->response->withBody(new StringStream($body));
}
}),
);
$engine->GET('/hello', (new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->Raw(HTTPStatus::StatusOK, 'hello world');
}
}));
$engine->setOnNotFound((new class implements ContextHandlerFuncInterface {
public function __invoke(Context $context)
{
$context->JSON(HTTPStatus::StatusOK, ['code' => HTTPStatus::StatusNotFound, 'msg' => 'not found']);
}
}));
$serv->setHandler($engine);
// server run
$serv->serve();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.