swoole引入laravel框架


在swoole中引入laravel之后,使用swoole写了一个httpserver,代码如下:


$http = new swoole_http_server('0.0.0.0',8811);

//进程启动配置
$http->set([
'enable_static_handler' => true,
'document_root' => '/mnt/lost+found/www/swoole_study/swoole_project/laravel/public/static',
'worker_num' => 2,
'max_request' => 5000,
]);

//工作进程启动
$http->on('WorkerStart',function($server, $worker_id) {
//加载index的内容
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../bootstrap/app.php';
});

//监听http请求
$http->on('request', function($request, $response) {

//释放$_SERVER
// if (!empty($_SERVER)) {
// unset($_SERVER);
// }

//优化$_SERVER常驻内存
$_SERVER = [];
//server信息
if (isset($request->server)) {
foreach ($request->server as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}

//header信息
if (isset($request->header)) {
foreach ($request->header as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}

//释放$_GET
// if (!empty($_GET)) {
// unset($_GET);
// }

//优化
$_GET = [];
//get请求
if (isset($request->get)) {
foreach ($request->get as $k => $v) {
$_GET[$k] = $v;
}
}

//释放$_POST
// if (!empty($_POST)) {
// unset($_POST);
// }

//优化
$_POST = [];
//post请求
if (isset($request->post)) {
foreach ($request->post as $k => $v) {
$_POST[$k] = $v;
}
}

//释放$_FILES
// if (!empty($_FILES)) {
// unset($_FILES);
// }

//优化
$_FILES = [];
//文件请求
if (isset($request->files)) {
foreach ($request->files as $k => $v) {
$_FILES[$k] = $v;
}
}

//释放$_COOKIE
// if (!empty($_COOKIE)) {
// unset($_COOKIE);
// }

//优化
$_COOKIE = [];
//cookie请求
if (isset($request->cookie)) {
foreach ($request->cookie as $k => $v) {
$_COOKIE[$k] = $v;
}
}

ob_start();//启用缓存区

//加载laravel请求核心模块
$kernel = app()->make(Illuminate\Contracts\Http\Kernel::class);
$laravelResponse = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$laravelResponse->send();
$kernel->terminate($request, $laravelResponse);

//获取控制器方法
$res = ob_get_contents();//获取缓存区的内容
ob_end_clean();//清除缓存区

//输出缓存区域的内容
$response->end($res);

//释放变量(简单粗暴的方式,还可以优化)
//$http->close();
});

$http->start();
然后经过路由访问:

namespace App\Http\Controllers\Study;
use App\Http\Controllers\Controller;
class IndexController extends Controller
{
public function test()
{
//测试获取请求参数
dd($_GET);
echo "hello laravel!!";
}

}

服务端开启httpserver : php http_server.php之后,通过http请求了一次,服务端出现:

Notice: Undefined index: argv in /mnt/lost+found/www/swoole_study/swoole_project/laravel/vendor/symfony/console/Input/ArgvInput.php on line 53


Warning: array_shift() expects parameter 1 to be array, null given in /mnt/lost+found/www/swoole_study/swoole_project/laravel/vendor/symfony/console/Input/ArgvInput.php on line 57


Warning: Invalid argument supplied for foreach() in /mnt/lost+found/www/swoole_study/swoole_project/laravel/vendor/symfony/console/Input/ArgvInput.php on line 276


Vote Vote Cancel Collect Collect Cancel

<< 上一篇: Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /data/www/billings/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:179 Stack trace: #0

>> 下一篇: 调用mysql 存储过程里面,不能操作打开关闭游标