控制器

功能介绍

毫无疑问,控制器层是负责处理客户端请求,转发给响应模型,并将结果返回给客户端。EasySwoole 使用了对象池复用模式,降低对象创建、销毁的开销,注入 requestresponse 对象来完成客户端与服务端之间的交互。

示例

App/HttpController/ 目录下增加文件 User.php

代码

<?php
/**
 * @CreateTime:   2020/8/19 12:30 上午
 * @Author:       huizhang  <2788828128@qq.com>
 * @Copyright:    copyright(2020) Easyswoole all rights reserved
 * @Description:  用户控制器
 */
namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class User extends Controller
{

    /**
     * 用户信息
     *
     * @return string
     * CreateTime: 2020/8/19 12:37 上午
     */
    public function userInfo()
    {
        // 获取 get 参数
        $name = $this->request()->getQueryParam('name');

        // 输出到终端
        var_dump($name);

        // 返回给客户端
        $this->response()->write($name . PHP_EOL);

        // return 返回的值会让框架在此进行控制器方法调度,将继续执行 User 控制器类的 requestTotal 方法 
        return '/User/requestTotal';
    }

    /**
     * 接口请求量
     *
     * CreateTime: 2020/8/19 12:37 上午
     */
    public function requestTotal()
    {
        $this->response()->write('请求数+1' . PHP_EOL);

        // 还可以 return,但不要两个方法互相调用,会导致死循环
    }

    /**
     * 此控制器抛异常时会执行此方法
     *
     * @param \Throwable $throwable
     * @throws \Throwable
     * CreateTime: 2020/8/19 12:48 上午
     */
    public function onException(\Throwable $throwable): void
    {
        parent::onException($throwable); // TODO: Change the autogenerated stub
    }

    /**
     * gc 方法将在执行完 afterAction 方法之后自动调用,可自行覆盖实现其他的 gc 回收逻辑
     *
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
    }

    /**
     * 当控制器方法执行结束之后将调用该方法,可自行覆盖该方法实现数据回收等逻辑
     *
     * @param string|null $actionName
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function afterAction(?string $actionName): void
    {
        parent::afterAction($actionName); // TODO: Change the autogenerated stub
    }

    /**
     * 当请求方法未找到时,自动调用该方法,可自行覆盖该方法实现自己的逻辑
     *
     * @param string|null $action
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function actionNotFound(?string $action)
    {
        parent::actionNotFound($action); // TODO: Change the autogenerated stub
    }

    /**
     * 所有控制器请求都会先经过该方法,如果此方法返回 false 则请求不继续往下执行,可用于权限验证
     *
     * @param string|null $action
     * @return bool|null
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function onRequest(?string $action): ?bool
    {
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }

}

执行过程

启动 easyswoole

php easyswoole.php server start

访问

curl http://localhost:9501/user/userInfo?name=easyswoole

执行结果

服务端输出

➜  doc-new git:(master) ✗ php easyswoole.php server start
#!/usr/bin/env php
  ______                          _____                              _
 |  ____|                        / ____|                            | |
 | |__      __ _   ___   _   _  | (___   __      __   ___     ___   | |   ___
 |  __|    / _` | / __| | | | |  \___ \  \ \ /\ / /  / _ \   / _ \  | |  / _ \
 | |____  | (_| | \__ \ | |_| |  ____) |  \ V  V /  | (_) | | (_) | | | |  __/
 |______|  \__,_| |___/  \__, | |_____/    \_/\_/    \___/   \___/  |_|  \___|
                          __/ |
                         |___/

main server                   SWOOLE_WEB
listen address                0.0.0.0
listen port                   9501
worker_num                    8
reload_async                  true
max_wait_time                 3
document_root                 /Users/guoyuzhao/sites/doc-new/Static
enable_static_handler         true
pid_file                      /Users/guoyuzhao/sites/doc-new/Temp/pid.pid
log_file                      /Users/guoyuzhao/sites/doc-new/Log/swoole.log
user                          guoyuzhao
swoole version                4.5.2
php version                   7.4.8
easyswoole version            3.4.0-dev
run mode                      dev
temp dir                      /Users/guoyuzhao/sites/doc-new/Temp
log dir                       /Users/guoyuzhao/sites/doc-new/Log
string(10) "easyswoole"

客户端输出

➜  ssh curl http://localhost:9501/user/userInfo\?name\=easyswoole

easyswoole
请求数+1

控制器方法

easyswoole 在控制器基类中实现了几个通用方法,当然用户也可根据需要进行方法重写实现自己的逻辑

onRequest

所有控制器请求都会先经过该方法,如果此方法返回 false 则请求不继续往下执行,可用于权限验证

protected function onRequest(?string $action): ?bool
{
    return true;
}

onException

当执行控制器方法抛异常时会调用该方法,可自行覆盖该方法实现异常捕获等逻辑

protected function onException(\Throwable $throwable): void
{
    throw $throwable;
}

afterAction

action 执行结束后调用该方法,可自行覆盖该方法实现数据回收等逻辑

protected function afterAction(?string $actionName): void
{

}

actionNotFound

当请求方法未找到时,自动调用此方法

protected function actionNotFound(?string $action)
{
    $class = static::class;
    $this->writeJson(\EasySwoole\Http\Message\Status::CODE_NOT_FOUND,null,"{$class} has not action for {$action}");
}

gc

gc 方法在 afterAction 方法执行完后调用

protected function gc()
{
    //恢复默认值
    foreach ($this->defaultProperties as $property => $value) {
        $this->{$property} = $value;
    }
}

注意事项

  • 只有第一次请求时才会调用构造函数
  • 对象池模式只重置非静态 public 属性
  • 对象池复用模式只针对单一进程,多个 worker 进程不共享
  • 文件夹、文件、类名为大驼峰,变量与类方法小驼峰(规范)
  • action 返回的字符串将会被 url 解析规则以及 route 路由规则解析
  • 两个 actionreturn 不能互相调用,否则将导致死循环

另外注意:在控制器类的方法(onRequest/action 等方法)中创建子协程,在子协程中使用 $this 的相关属性值时必须使用 use 引入,不使用 use 引入时将导致协程上下文数据错乱。

错误使用示例:

下面以在 Index 控制器类中的 action(index) 中使用为示例:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Utility\Random;

class Index extends Controller
{
    public function index()
    {
        // 设置请求标识
        $requestFlag = Random::number(3);
        $this->request()->withAttribute('requestFlag', $requestFlag);
        $rq = '第 ' . $this->request()->getRequestParam('times') . ' 次请求:';
        var_dump($rq . $this->request()->getAttribute('requestFlag'));
        go(function () {
            $rq = '第 ' . $this->request()->getRequestParam('times') . ' 次请求:';
            go(function () {
                $rq = '第 ' . $this->request()->getRequestParam('times') . ' 次请求:';
                \co::sleep(2);
                var_dump($rq . $this->request()->getAttribute('requestFlag'));
            });
            \co::sleep(4);
            // 【这里的数据会错乱】
            var_dump($rq . $this->request()->getAttribute('requestFlag'));
        });
        $this->response()->write('this is index!' . $this->request()->getRequestParam('times'));
    }
}

然后我们访问 http://127.0.0.1:9501/?times=1(示例请求地址),隔 1s 后我们再次访问 http://127.0.0.1:9501/?times=2(示例请求地址),发现出现如下运行结果,控制台输出结果:

string(21) "第 1 次请求:765"
string(21) "第 1 次请求:765"
string(21) "第 2 次请求:823"
string(21) "第 1 次请求:823"
string(21) "第 2 次请求:823"
string(21) "第 2 次请求:823"

发现和我们想象中的完全不一样,第 1 次请求挂载的数据被“污染”了,因为 EasySwoole 控制器采用的是对象池模式。

正确使用方式如下:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Utility\Random;

class Index extends Controller
{
    public function index()
    {
        // 设置请求标识
        $requestFlag = Random::number(3);
        $this->request()->withAttribute('requestFlag', $requestFlag);
        $rq = '第 ' . $this->request()->getRequestParam('times') . ' 次请求:';
        var_dump($rq . $this->request()->getAttribute('requestFlag'));
        go(function () use ($rq, $requestFlag) {
            go(function () use ($rq, $requestFlag) {
                \co::sleep(2);
                var_dump($rq . $requestFlag);
            });
            \co::sleep(4);
            // 【这里的数据会错乱】
            var_dump($rq . $requestFlag);
        });
        $this->response()->write('this is index!' . $this->request()->getRequestParam('times'));
    }
}

然后我们访问 http://127.0.0.1:9501/?times=1(示例请求地址),隔 1s 后我们再次访问 http://127.0.0.1:9501/?times=2(示例请求地址),发现出现如下运行结果,控制台输出结果:

string(21) "第 1 次请求:690"
string(21) "第 1 次请求:690"
string(21) "第 2 次请求:820"
string(21) "第 1 次请求:690"
string(21) "第 2 次请求:820"
string(21) "第 2 次请求:820"

发现数据正常了。