控制器
功能介绍
毫无疑问控制器层是负责处理客户端请求,转发给响应模型,并将结果返回,es使用了对象池复用模式降低对象创建、销毁的开销,注入request
和response
对象来完成客户端与服务端之间的交互。
示例
在
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返回的值会让框架在此进行控制器方法调度
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
}
/**
* 当控制器逻辑抛出异常时将调用该方法进行处理异常(框架默认已经处理了异常)可覆盖该方法,进行自定义的异常处理
*
* @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 server start
访问
curl http://localhost:9501/user/userInfo?name=easyswoole
执行结果
服务端输出
➜ doc-new git:(master) ✗ php easyswoole 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
控制器方法
es 在控制器基类中实现了几个通用方法,当然用户也可根据需要进行方法重写实现自己的逻辑
onRequest
所有控制器请求都会先经过该方法,如果此方法返回false则请求不继续往下执行,可用于权限验证
protected function onRequest(?string $action): ?bool
{
return true;
}
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属性
- 对象池复用模式只针对单一进程,多个work进程不共享
- 文件夹、文件、类名为大驼峰,变量与类方法小驼峰(规范)
- action返回的字符串将会被url解析规则以及route路由规则解析
- 两个action的return不能互相调用,否则死循环