共计 4899 个字符,预计需要花费 13 分钟才能阅读完成。
前言
官网地址:SW- X 框架 - 专一高性能便捷开发而生的 PHP-SwooleX 框架
心愿各大佬举起小手,给小弟一个 star:https://github.com/swoolex/swoolex
1、404 接口配置
HTTP 服务中,当查找不到对应的路由时,会加载 /config/route.php
配置文件中的 404 配置。
以下为具体的参数配置项:
<?php
return [
// 找不到路由的时候,是否启用自定义的谬误处理程序
'404' => true,
// 自定义的谬误处理程序,只须要提供一个类的命名空间地位即可
'error_class' => '\\box\\http_error',
];
上面咱们关上 /box/http_error.php
文件,并批改成以下代码:
<?php
namespace box;
use \x\controller\Http;
class http_error extends Http{
/**
* 入口
* @todo 无
* @author 小黄牛
* @version v1.0.1 + 2020.05.29
* @deprecated 暂不启用
* @global 无
* @return void
*/
public function __construct() {return \x\Restful::code(\x\Restful::ONROUTE())->callback();}
}
因为下面应用 \x\Restful
组件,抛出了一个 ONROUTE
状态码,所以对应的 Restful 配置也要进行批改:
/restful/default/code.php
批改成如下代码:
<?php
return [
'ERROR' => 0, // 默认失败状态码
'SUCCESS' => 1, // 默认胜利状态码
'ACTION_ERROR' => 40001, // 权限校验失败
'ONROUTE' => 40004, // 不存在的接口
];
/restful/default/msg.php
批改成如下代码:
<?php
return [
// 默认谬误状态码对应的 tips
'ERROR' => ['default' => '申请失败', // 默认值],
// 默认胜利状态码对应的 tips
'SUCCESS' => [
'default' => '申请胜利', // 默认值
'test' => '测试 msg',
],
// 权限校验失败
'ACTION_ERROR' => ['default' => '权限校验失败', // 默认值],
// 不存在的接口
'ONROUTE' => ['default' => '该接口不存在', // 默认值],
];
这时候咱们轻易拜访一个不存在的接口,http://IP 地址: 端口 /api/user/info
,就能够看到框架抛出了上面的状态码:
{
"code": 40004,
"msg": "该接口不存在",
"data": []}
2、申请类型限度
SW- X 中须要限度某个 HTTP 控制器的申请类型有多种形式,
第 1 种是应用 @Post
、@Get
、@Ajax
注解进行限度。
第 2 种,则是应用 \x\Validate
验证器组件,对申请头进行判断过滤。
个别倡议应用第 1 种形式,因为非常灵活,而第 2 种个别是用于对某些同组接口须要进行雷同申请头过滤,例如签名判断等业务时应用。
上面,咱们将 /app/http/v1_0_1/controller/shop/delete.php
控制器改代码如下:
<?php
namespace app\http\v1_0_1\controller\shop;
use x\controller\Http;
// 引入 Restful 组件
use x\Restful;
class delete extends Http
{
/**
* 我只容许 Post+Ajax 的申请
* @Post
* @Ajax
*/
public function index() {
// Restful 组件抛出接口响应
return Restful::code(Restful::SUCCESS())->data('您的申请通过啦')->callback();}
}
同时,因为该注解的错误信息是由 /box/lifecycle/route_error.php
生命周期文件进行回调解决,所以咱们还须要将该文件改成以下代码:
<?php
namespace box\lifecycle;
class route_error
{
/**
* 非凡转换 3 种注解的 Msg 值
*/
private $tips = [
'Route Method Get' => '只容许 Get 申请',
'Route Method Post' => '只容许 Post 申请',
'Route Method Ajax' => '只容许 Ajax 申请',
];
/**
* 承受回调解决
* @todo 无
* @author 小黄牛
* @version v1.1.5 + 2020.07.15
* @deprecated 暂不启用
* @global 无
* @param Swoole $server 服务实例
* @param string $fd 客户端标识
* @param string $status 谬误事件状态码
* @return bool
*/
public function run($server, $fd, $status) {
$tips = 'Annotate:SW-X Status:'.$status.'ERROR!';
$type = \x\Config::get('server.sw_service_type');
// 只解决 HTTP 服务申请的抛出
if ($type == 'http') {if (isset($this->tips[$status])) $tips = $this->tips[$status];
return \x\Restful::code(\x\Restful::LIFECYCLE())->setMsg($tips)->callback();}
return true;
}
}
因为下面应用 \x\Restful
组件,抛出了一个 LIFECYCLE
状态码,所以对应的 Restful 配置也要进行批改:
/restful/default/code.php
批改成如下代码:
<?php
return [
'ERROR' => 0, // 默认失败状态码
'SUCCESS' => 1, // 默认胜利状态码
'ACTION_ERROR' => 40001, // 权限校验失败
'ONROUTE' => 40004, // 不存在的接口
'LIFECYCLE' => 20001, // 路由生命周期谬误回调状态码[应用自定义 MSG]
];
留神,生命周期文件批改后,热重载有效,须要手动 stop 后,从新 start 服务。
实现重启后,咱们再拜访 http://IP 地址: 端口 /api/shop/delete
接口,就能够看到框架抛出了上面的状态码:
{
"code": 20001,
"msg": "只容许 Post 申请",
"data": []}
如果要测试申请胜利,能够应用 Postman 或 Jquery 等工具,发送 Post-Ajax 申请即可。
3、Validate 校验表单数据
SW- X 种举荐应用 \x\Validate
验证器组件,对申请进行判断过滤。
具体的应用办法,能够参考官网文档:https://www.sw-x.cn/word/v2.5.x/vali_explain.html
上面咱们来创立一个 Shop
校验器,用于针对商品分组的 API 申请。
创立验证器文件/box/validate/Shop.php
,写入代码如下:
<?php
namespace box\validate;
use x\Validate;
class Shop extends Validate
{
// 定义字段对应的规定
protected $rule = [
'id' => 'require|int',
'title' => 'require|min:10|max:20',
'info.des' => 'min:10|max:20',
];
// 自定义谬误值申明
protected $message = ['id.require' => '{id}遗记输出啦',
'title.min' => '{title}不能小于{0}',
'title.max' => '{title}不能大于{0}',
'info.des.min' => '{:preset}不能小于{0}',
'info.des.min' => '{info.des}不能大于{0}',
];
// 场景定义
protected $scene = [
// 批改时应用的场景(简写)'edit' => ['id','info.des'], // 须要校验的字段
// 删除时应用的场景(简写)'delete' => ['id'], // 须要校验的字段
];
}
上面,咱们将 /app/http/v1_0_1/controller/shop/delete.php
控制器改代码如下:
<?php
namespace app\http\v1_0_1\controller\shop;
use x\controller\Http;
// 引入 Restful 组件
use x\Restful;
class delete extends Http
{
/**
* 应用注解绑定校验器
* @Validate(class="\box\validate\Shop", scene="delete")
*/
public function index() {
// Restful 组件抛出接口响应
return Restful::code(Restful::SUCCESS())->data('校验通过啦')->callback();}
}
同时,因为该注解的错误信息是由 /box/lifecycle/validate_error.php
生命周期文件进行回调解决,所以咱们还须要将该文件改成以下代码:
<?php
namespace box\lifecycle;
class validate_error
{
/**
* 承受回调解决
* @todo 无
* @author 小黄牛
* @version v2.5.6 + 2021-09-15
* @deprecated 暂不启用
* @global 无
* @param string $server_type 服务类型 http/websocket/rpc/mqtt
* @param bool $batch 是否全副过滤
* @param array $errors 谬误验证后果集
* @return bool
*/
public function run($server_type, $batch, $errors) {
// $batch 是用于执行全副过滤规定, 再最初一起返回全副的谬误起因
// 默认生命周期只返回第一个谬误起因
$error = $errors[0]['intact_field'].'=>'.$errors[0]['message'];
// 只解决 HTTP 服务申请的抛出
if ($server_type == 'http') {return \x\Restful::code(\x\Restful::VALIDATE())->setMsg($errors[0]['message'])->callback();}
return true;
}
}
因为下面应用 \x\Restful
组件,抛出了一个 VALIDATE
状态码,所以对应的 Restful 配置也要进行批改:
/restful/default/code.php
批改成如下代码:
<?php
return [
'ERROR' => 0, // 默认失败状态码
'SUCCESS' => 1, // 默认胜利状态码
'ACTION_ERROR' => 40001, // 权限校验失败
'ONROUTE' => 40004, // 不存在的接口
'LIFECYCLE' => 20001, // 路由生命周期谬误回调状态码[应用自定义 MSG]
'VALIDATE' => 20002, // 校验器生命周期谬误回调状态码[应用自定义 MSG]
];
留神,生命周期文件批改后,热重载有效,须要手动 stop 后,从新 start 服务。
实现重启后,咱们再拜访 http://IP 地址: 端口 /api/shop/delete
接口,就能够看到框架抛出了上面的状态码:
{
"code": 20002,
"msg": "id 遗记输出啦",
"data": []}
拜访 http://IP 地址: 端口 /api/shop/delete?id=1
,就能够通过delete
的校验场景了。