看完这篇文章,就知道给 Laravel 开启 Swoole还可以不用三方包

Swoole 是一款优秀的 PHP 扩展,利用其可以实现原生 PHP 很难做到的常驻服务和异步。正好我有个 Laravel 项目可以折腾,就研究了下。

点此加入我的企鹅群

Laravel 项目是基于 composer 的,所以我先帖下我的 composer.json 中的 require 声明:

{
    "require": {
        "php": "^7.1.3",
        "cybercog/laravel-love": "^5.1",
        "dingo/api": "~v2.0.0-alpha2",
        "doctrine/dbal": "^2.8",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "infyomlabs/adminlte-templates": "5.6.x-dev",
        "infyomlabs/laravel-generator": "5.6.x-dev",
        "jeroennoten/laravel-adminlte": "^1.23",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "laravelcollective/html": "^5.6.0",
        "lshorz/luocaptcha": "^1.0",
        "overtrue/laravel-lang": "v3.0.08",
        "overtrue/laravel-wechat": "^4.0",
        "predis/predis": "^1.1",
        "spatie/laravel-permission": "^2.17",
        "tymon/jwt-auth": "~1.0.0-rc.2",
        "yajra/laravel-datatables-buttons": "^4.0",
        "yajra/laravel-datatables-oracle": "^8.7"
    }
}

如果我们要开启 swoole,我们可选的包有这些:

swooletw/laravel-swoole

hhxsv5/laravel-s

但一般来说,项目中需要常驻容器的服务与每次均需重新构建的服务并不一样,所以我才剑走偏锋。

起步

我们需要将 public/index.php 替换成如下

<?php

use Illuminate\Http\Request;
use Illuminate\Http\Response;

define('LARAVEL_START', microtime(true));
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';

class Laravel
{
    /**
     * Illuminate\Foundation\Application
     *
     * @var \Illuminate\Foundation\Application
     */
    public $app;

    /**
     * App\Http\Kernel
     *
     * @var \App\Http\Kernel
     */
    public $kernel;

    /**
     * App\Http\Requests\Request
     *
     * @var \App\Http\Requests\Request
     */
    public $request;

    /**
     * Illuminate\Http\JsonResponse
     *
     * @var \Illuminate\Http\JsonResponse
     */
    public $response;

    /**
     * 构造
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct(\Illuminate\Foundation\Application $app)
    {
        $this->app = $app;
    }

    /**
     * RUN
     *
     * @return void
     */
    public function run()
    {
        \Swoole\Runtime::enableCoroutine(true);

        $http = new swoole_http_server('127.0.0.1', '80');

        $http->set([
            'document_root' => public_path('/'),
            'enable_static_handler' => true,
        ]);

        $http->on('request', function ($req, $res) {
            try {
                $kernel = $this->app->make(Illuminate\Contracts\Http\Kernel::class);

                $get = $req->get ?? [];
                $post = $req->post ?? [];
                $input = array_merge($get, $post);
                $cookie = $req->cookie ?? [];
                $files = $req->files ?? [];
                $server = $req->server ?? [];

                $request = Request::create($req->server['request_uri'], $req->server['request_method'], $input, $cookie, $files, $server);

                if (isset($req->header['x-requested-with']) && $req->header['x-requested-with'] == "XMLHttpRequest") {
                    $request->headers->set('X-Requested-With', "XMLHttpRequest", true);
                }
                if (isset($req->header['accept']) && $req->header['accept']) {
                    $request->headers->set('Accept', $req->header['accept'], true);
                }

                $response = $kernel->handle($request);

                $res->status($response->getStatusCode());

                foreach ($response->headers->allPreserveCaseWithoutCookies() as $name => $values) {
                    foreach ($values as $value) {
                        $res->header($name, $value, false);
                    }
                }

                foreach ($response->headers->getCookies() as $cookie) {
                    $res->header('Set-Cookie', $cookie->getName() . strstr($cookie, '='), false);
                }
                dump(time());

                $res->end($response->getContent());
                $this->app->forgetInstance('request');
            } catch (\throwable $e) {
                echo $e->getMessage();
                echo PHP_EOL;
                echo $e->getFile();
                echo PHP_EOL;
                echo $e->getLine();
                echo PHP_EOL;
            }
        });
        $http->start();
    }
}

(new Laravel($app))->run();

运行时发现大多数页面均没有问题,只有几个用了 infyomlabs/laravel-generator 产生的列表页,AJAX 拉取 JSON 时却返回了 HTML。

排查

在有问题页面的 controller 代码中,找到如下

/**

  • Display a listing of the Star.
  • @param StarDataTable $starDataTable
  • @return Response
    */
    public function index(StarDataTable starDataTable) { returnstarDataTable->render('stars.index');
    }

定位 StarDataTable::render() 到了

   /**
     * Process dataTables needed render output.
     *
     * @param string $view
     * @param array $data
     * @param array $mergeData
     * @return mixed
     */
    public function render($view, $data = [], $mergeData = [])
    {
        if ($this->request()->ajax() && $this->request()->wantsJson()) {
            return app()->call([$this, 'ajax']);
        }
        ...
    }

这是判断 $this->request() 是不是 XHR 请求,且 Accept 请求头声明了 application/json

$this->request() 实现如下

/**

  • Get DataTables Request instance.
  • @return \Yajra\DataTables\Utilities\Request
    */
    public function request()
    {
    return this->request ?:this->request = resolve('datatables.request');
    }

不难看出,如果第一次构建,会走到

$this->request = resolve('datatables.request');

而 resolve 的实现是啥?

if (! function_exists('resolve')) {
    /**
     * Resolve a service from the container.
     *
     * @param  string  $name
     * @return mixed
     */
    function resolve($name)
    {
        return app($name);
    }
}

就是从容器中取出 datatables.request 的过程。

所以我们只需让每次请求结束,$app 容器忘掉 datatables.request 就好了

改进

增加遗忘 datatables.request

res->end(response->getContent());
this->app->forgetInstance('request');this->app->forgetInstance('datatables.request');
$this->app->forgetInstance(\Dingo\Api\Http\Middleware\Request::class);

完整最终版:

<?php

use Illuminate\Http\Request;
use Illuminate\Http\Response;

define('LARAVEL_START', microtime(true));
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';

class Laravel
{
    /**
     * Illuminate\Foundation\Application
     *
     * @var \Illuminate\Foundation\Application
     */
    public $app;

    /**
     * App\Http\Kernel
     *
     * @var \App\Http\Kernel
     */
    public $kernel;

    /**
     * App\Http\Requests\Request
     *
     * @var \App\Http\Requests\Request
     */
    public $request;

    /**
     * Illuminate\Http\JsonResponse
     *
     * @var \Illuminate\Http\JsonResponse
     */
    public $response;

    /**
     * 构造
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct(\Illuminate\Foundation\Application $app)
    {
        $this->app = $app;
    }

    /**
     * RUN
     *
     * @return void
     */
    public function run()
    {
        \Swoole\Runtime::enableCoroutine(true);

        $http = new swoole_http_server('127.0.0.1', '80');

        $http->set([
            'document_root' => public_path('/'),
            'enable_static_handler' => true,
        ]);

        $http->on('request', function ($req, $res) {
            try {
                $kernel = $this->app->make(Illuminate\Contracts\Http\Kernel::class);

                $get = $req->get ?? [];
                $post = $req->post ?? [];
                $input = array_merge($get, $post);
                $cookie = $req->cookie ?? [];
                $files = $req->files ?? [];
                $server = $req->server ?? [];

                $request = Request::create($req->server['request_uri'], $req->server['request_method'], $input, $cookie, $files, $server);

                if (isset($req->header['x-requested-with']) && $req->header['x-requested-with'] == "XMLHttpRequest") {
                    $request->headers->set('X-Requested-With', "XMLHttpRequest", true);
                }
                if (isset($req->header['accept']) && $req->header['accept']) {
                    $request->headers->set('Accept', $req->header['accept'], true);
                }

                $response = $kernel->handle($request);

                $res->status($response->getStatusCode());

                foreach ($response->headers->allPreserveCaseWithoutCookies() as $name => $values) {
                    foreach ($values as $value) {
                        $res->header($name, $value, false);
                    }
                }

                foreach ($response->headers->getCookies() as $cookie) {
                    $res->header('Set-Cookie', $cookie->getName() . strstr($cookie, '='), false);
                }
                dump(time());

                $res->end($response->getContent());
                $this->app->forgetInstance('request');
                //$this->app->forgetInstance('session');
                //$this->app->forgetInstance('session.store');
                //$this->app->forgetInstance('cookie');
                $this->app->forgetInstance('datatables.request');
                $this->app->forgetInstance(\Dingo\Api\Http\Middleware\Request::class);
                //$kernel->terminate($request, $response);
            } catch (\throwable $e) {
                echo $e->getMessage();
                echo PHP_EOL;
                echo $e->getFile();
                echo PHP_EOL;
                echo $e->getLine();
                echo PHP_EOL;
            }
        });
        $http->start();
    }
}

(new Laravel($app))->run();

测试

比原生 laravel 确实快不少(这还有 4 句 SQL 查询)

在这里插入图片描述
  • 注,此处给出的代码可以借鉴,但未经长期验证。且不同项目实际用到的包不同,需要在调试过程中 debug 容器中的服务提供者,和追踪代码来调优。

已知问题

  • flash 闪存数据以及表单验证错误的展示有问题

  • PDO 会报 Cannot execute queries while other unbuffered queries are active - https://github.com/symfony/symfony/issues/6745

  • Throttle 的 IP 获取设定默认会产生问题

点关注,不迷路

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才。之前说过,PHP方面的技术点很多,也是因为太多了,实在是写不过来,写过来了大家也不会看的太多,所以我这里把它整理成了PDF和文档,如果有需要的可以

点击进入暗号: PHP+「平台」

在这里插入图片描述
在这里插入图片描述

更多学习内容可以访问【对标大厂】精品PHP架构师教程目录大全,只要你能看完保证薪资上升一个台阶(持续更新)

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的 PHP技术交流群

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 225,498评论 6 524
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,668评论 3 406
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 172,857评论 0 370
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,305评论 1 303
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,308评论 6 401
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,747评论 1 316
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,078评论 3 431
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,080评论 0 280
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,649评论 1 327
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,644评论 3 347
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,760评论 1 355
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,352评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,076评论 3 341
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,490评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,651评论 1 277
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,353评论 3 383
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,828评论 2 367

推荐阅读更多精彩内容