(1):http api的基本套路
经常要翻阅的文档
现在我们举个例子:
Product商品项目,拟定的API如下
GET /product ------显示商品列表
GET /product/123 -------显示商品ID为123的商品详情
Controller 控制器
控制器作为HTTP服务的核心组件,串接起一次请求的整个生命周期. 通过 注解 的方式,相较于传统的 Controller,代码更简洁,用户可以更关注业务逻辑。
创建控制器
主要通过 @Controller
注解实现。代码可以放置任意位置,不过为了统一标准,建议放在 app/Http/Controller
下
可以通过 swoftcli
快速创建新的控制器
php swoftcli.phar gen:http-ctrl Product --prefix /Product
(我这里已经将swoftcli.phar 修改成别名 swoftcli于是直接执行swoftcli gen:http-ctrl Product --prefix /Product即可)
执行此命令可以在app\Http\Controller下创建一个名为 ProductController.php的控制器文件,路由前缀为/Product
当然我们也可以手动进行创建控制器;
例如我们手动创建个Http的 Product.php控制器文件
@Controller 注解
Http 控制器类注解 @Controller
- 注解类:
Swoft\Http\Server\Annotation\Mapping\Controller
- 作用范围:
CLASS
- 拥有属性:
prefix
指定路由前缀
通常仅有
@Controller
是没有什么效果的,它需要配合接下来的@RequestMapping
一起才能正确的工作。
路由规则
- 显式指定路由前缀:
@Controller(prefix="/index")
或@Controller("/index")
。 - 隐式指定路由前缀:
@Controller()
默认自动使用小驼峰
格式解析controller class
的名称。 - 一个完整的路由规则是通过
@Controller
+@RequestMapping
注解实现,通常前者定义前缀,后者定义后缀。关于@RequestMapping
注解将在稍后 路由-@RequestMapping 章节将会详细介绍;
示例: 根据下方的定义,对应的访问路由是 /v1/users/list
(/v1/users
+ list
)
/** * @Controller(prefix="/v1/users") */ class UsersController { /** * @RequestMapping(route="list") */ public function list(){} }
示例: 若 @Controller()
参数为空,则会使用隐式路由前缀绑定,例如下方的定义,对应的访问路由是 /user/list
/** * @Controller() */ class UsersController { /** * @RequestMapping(route="list") */ public function list(){} }
***警告***在 Swoft 里不要按照传统的 fpm 框架继承父类控制器的成员属性在其他控制器使用,这种做法是错误的。
错误示范:
/** * @Controller() */ class BaseController { protected $num; } /** * @Controller(prefix="/v1/index") */ class IndexController extends BaseController { /** * @RequestMapping(route="index") */ public function index() { $this->num++; echo $this->num."\n"; } }
(2):
路由
Swoft 与传统的 PHP 框架不一样,并没有采用配置文件的方式来配置路由,而采用了注解。在 Swoft 里我们可以使用 @RequestMapping
注解快速的添加路由。
路由配置
这是默认的路由配置
// at file: vendor/swoft/http-server/src/AutoLoader.php 'httpRouter' => [ 'name' => 'swoft-http-router', // config 'ignoreLastSlash' => true, 'tmpCacheNumber' => 500, // 'handleMethodNotAllowed' => false ],
配置说明
-
ignoreLastSlash
bool 默认:true
是否忽略 URI path 最后的/
- 如果设置为
false
不忽略,/home
与/home/
将是两个不同的路由
- 如果设置为
-
tmpCacheNumber
int 默认:500
动态路由缓存数量。- 动态参数路由匹配后会缓存下来,下次相同的路由将会更快的匹配命中。
-
handleMethodNotAllowed
bool 默认:false
是否处理MethodNotAllowed
- 为了加快匹配速度,默认
method
不匹配也是直接抛出Route not found
错误。如有特殊需要可以开启此选项,开启后将会抛出Method Not Allowed
错误
- 为了加快匹配速度,默认
若你需要自定义路由配置,直接在 app/bean.php
添加 httpRouter
项配置即可。
'httpRouter' => [ 'handleMethodNotAllowed' => true ]
@RequestMapping 注解
Http 控制器类中方法路由注解 @RequestMapping
route
路由规则pathmethod
请求方式(GET、POST、PUT、PATCH、DELETE、OPTIONS、HEAD)params
可以通过它为path变量添加正则匹配限制
路由规则
- 通常情况,一个完整的路由
path
等于@Controller
的prefix
+@RequestMapping
的route
特殊的,当你的@RequestMapping
上的路由以/
开头时,那完整的路由就是它,即不会再将prefix
添加到它的前面- 显示指定路由后缀:
@RequestMapping("index")
或@RequestMapping(route="index")
- 隐式指定路由后缀: 使用
@RequestMapping()
默认解析方法名为后缀
- 显示指定路由后缀:
- 特殊的,当你的
@RequestMapping
上的路由以/
开头时,那完整的路由就是它,即不会再将prefix
添加到它的前面
示例: 在控制器方法中加入 @RequestMapping
注解
/** * @Controller() */ class UserController { /** * @RequestMapping() */ public function index() {} /** * @RequestMapping("index") */ public function index() {} /** * @RequestMapping(route="index") */ public function index() {} }
代码执行后将会为
index
方法绑定路由为 /user/index
,允许的请求方法为默认的 GET
和 POST;
绑定路由 path 参数
- 指定路由参数:
@RequestMapping(route="index/{name}")
,Action 方法中可以直接使用$name
作为方法参数 - 当路由参数被
[]
包起来则 URL path 传递参数是可选的。注意,可选符只能用在最后面- 示例1:
@RequestMapping("/index[/{name}]")
这样/index`
/index/tom` 都可以访问到 - 示例2:
@RequestMapping("/about[.html]")
相当于伪静态,/about`
/about.html` 都可以访问到
- 示例1:
设置路由请求方式
如果想要设置允许请求控制器的 HTTP 请求方式。 可以使用方法在控制器中的 @RequestMapping
注解配置 method
参数,可以是 GET
、POST
、PUT
、PATCH
、DELETE
、OPTIONS
、HEAD
中的一个或多个。
- 限定
HTTP
方法:@RequestMapping(method={RequestMethod::GET})
指定路由支持的HTTP
方法,默认是支持GET
和POST
请切记要引入相关的注解类
Swoft\Http\Server\Annotation\Mapping\RequestMapping
Swoft\Http\Server\Annotation\Mapping\RequestMethod
获取路由匹配结果
你可以在中间件或者 action
拿到路由匹配的结果信息。
[$status, $path, $route] = $request->getAttribute(Request::ROUTER_ATTRIBUTE);
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="product",method={RequestMethod::GET})
*/
//use Swoft\Http\Message\Request; 引入Request类;
//方法注入
public function prod_list(Request $request)
{
return $request->getAttribute(Request::ROUTER_ATTRIBUTE); //返回结果值 [1,"\/product\/product",{}]
}
}
Http 请求对象
Swoft 的请求与响应实现于 PSR-7
规范。请求与响应对象存在于每次 HTTP
请求。
- 请求对象
Request
为Swoft\Http\Message\Request
- 响应对象
Response
为Swoft\Http\Message\Response
PSR-7 接口为请求和响应对象提供了这些公共方法:
withProtocolVersion($version)
withHeader($name, $value)
withAddedHeader($name, $value)
withoutHeader($name)
withBody(StreamInterface $body)
PSR-7 接口为请求对象提供了这些方法:
withMethod(string $method)
withUri(UriInterface $uri, $preserveHost = false)
withCookieParams(array $cookies)
withQueryParams(array $query)
withUploadedFiles(array $uploadedFiles)
withParsedBody($data)
withAttribute($name, $value)
withoutAttribute($name)
更多请参考 PSR-7 和 查看 swoft/http-message 中具体的实现类
***警告真相****:根据PSR-7对象的不可变性(immutable),所有的 with* 方法都是克隆对象然后返回,必须接收新对象来做进一步处理,或使用链式调用
获取请求对象
- 通过控制器方法参数注入
public function action(Request $request)
- 通过请求上下文获取
Swoft\Context\Context::mustGet()->getRequest()
//已经过期 推荐使用context()->getRequest();
示例: 获取请求动作
$request = context()->getRequest(); $method = $request->getMethod();
代码演示:
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="prod_context",method={RequestMethod::GET,RequestMethod::POST})
* @throws \Swoft\Exception\SwoftException
*/
public function prod_context()
{
$request = context()->getRequest();
$method = $request->getMethod();
return $method;// 返回结果:GET
}
}
示例: 获取请求的 URI
每个 HTTP 请求都有一个 URI
标识所请求的应用程序资源。HTTP 请求 URI
有几个部分:
Scheme
(e.g. http or https)Host
(e.g. example.com)Port
(e.g. 80 or 443)Path
(e.g. /users/1)Query
string (e.g. sort=created&dir=asc)
你可以通过请求对象的 getUri()
方法获取 PSR-7 URI对象:
$method = $request->getUri();
PSR-7
请求对象的 URI
本身就是一个对象,它提供了下列方法检查 HTTP 请求的 URL 部分
getScheme()
getAuthority()
getUserInfo()
getHost()
getPort()
getPath()
getQuery() (e.g. a=1&b=2)
getFragment()
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="get_uri")
* @throws \Swoft\Exception\SwoftException
*/
public function get_uri()
{
$uriInfo=array();
$request = context()->getRequest();
$getUri = $request->getUri();
$getScheme=$request->getUri()->getScheme();
$getAuthority=$request->getUri()->getAuthority();
$getUserInfo=$request->getUri()->getUserInfo();
$getHost=$request->getUri()->getHost();
$getPort=$request->getUri()->getPort();
$getPath = $request->getUri()->getPath();
$getQuery=$request->getUri()->getQuery();// (e.g. a=1&b=2);
$getFragment=$request->getUri()->getFragment();
//请求路径 http://192.168.1.120:9500/product/get_uri?a=1&b=2
$uriInfo['getUri']=$getUri;//getUri {}
$uriInfo['getScheme']=$getScheme;//getScheme http
$uriInfo['getAuthority']=$getAuthority;//getAuthority 192.168.1.120:9500
$uriInfo['getUserInfo']=$getUserInfo;//getUserInfo
$uriInfo['getHost']=$getHost;//getHost 192.168.1.120
$uriInfo['getPort']=$getPort;//getPort 9500
$uriInfo['getPath']=$getPath;//getPath /product/get_uri
$uriInfo['getQuery']=$getQuery;//getQuery a=1&b=2
$uriInfo['getFragment']=$getFragment;//getFragment
return json_encode($uriInfo);
}
}
示例: 获取请求 Headers
全部的 Headers
$headers = $request->getHeaders(); foreach ($headers as $name => $values) { echo $name . ": " . implode(", ", $values).PHP_EOL; }
指定的 Header
$headerValueArray = $request->getHeader('host'); print_r($headerValueArray); // return Array $host = $request->getHeaderLine("host"); print_r($host); // return String
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="get_header",method={RequestMethod::GET})
* @throws \Swoft\Exception\SwoftException
*/
public function get_header()
{
$request = context()->getRequest();
$headers = $request->getHeaders();
$headerValueArray = $request->getHeader('host');
$host = $request->getHeaderLine("host");
print_r($headerValueArray); // return Array
print_r($host); // return String
/*Array
(
[0] => 192.168.1.120:9500
)
192.168.1.120:9500*/
}
GET 数据
$data = $request->query(); $some = $request->query('key', 'default value') $data = $request->get(); $some = $request->get('key','default value');
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="get_query")
* @throws \Swoft\Exception\SwoftException
*/
public function get_query()
{
//请求路径:http://192.168.1.122:9500/product/get_query?a=7
$request=context()->getRequest();
/* $data = $request->query();
$some = $request->query('a',"ttt");
return $some;//返回值 7*/
$data = $request->get();
$some = $request->get('a','999');
return $some;//返回值 7
}
POST 数据
$data = $request->post(); $some = $request->post('key', 'default value')
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="post_query")
* @throws \Swoft\Exception\SwoftException
*/
public function post_query()
{
$request=context()->getRequest();
$data = $request->post();
//return $data; 返回值数组
/* {
"name": "痞子胥",
"age": "30",
"sex": "男"
}*/
$some = $request->post('name', 'default value');
return $some; //返回值数组
/* {
"data": "痞子胥"
}*/
}
无需关心请求的数据格式,
json`
xml请求都会自动解析为
php的数组数据。都可以通过
$request->post()` 获取。
GET & POST 数据
$data = $request->input(); $some = $request->input('key', 'default value')
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="get_post_query")
* @throws \Swoft\Exception\SwoftException
*/
public function get_post_query()
{
//post或者get提交数据的通用写法
$request=context()->getRequest();
$data = $request->input();
return $data;
//$some = $request->input('key', 'default value');
//return $some;
}
}
RAW 数据
$data = $request->raw();
SERVER 数据
$data = $request->getServerParams(); $some = $request->server('key', 'default value');
<?php
namespace App\Http\Controller;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
/**
* 商品模块
* @Controller(prefix="/product")
*/
class Product
{
/**
* @RequestMapping(route="get_raw")
* @throws \Swoft\Exception\SwoftException
*/
public function get_server()
{
$request=context()->getRequest();
$data = $request->getServerParams();
return $data;
//返回数据
/* {
"request_method": "POST",
"request_uri": "/product/get_raw",
"path_info": "/product/get_raw",
"request_time": 1578108536,
"request_time_float": 1578108536.066696,
"server_protocol": "HTTP/1.1",
"server_port": 18306,
"remote_port": 55453,
"remote_addr": "192.168.1.108",
"master_time": 1578108535
}*/
}
获取上传文件
$file = $request->getUploadedFiles();
获取的结果是一维数组或者二位数组,数据结构如下。 若表单中上传的是单文件则返回的是一个一维数组,数组内容是 Swoft\Http\Message\Upload\UploadedFile
文件对象,例如文件字段名为 file
则数据结构如下
array(1) { ["file"]=> object(Swoft\Http\Message\Upload\UploadedFile)#6510 (7) { ["size":"Swoft\Http\Message\Upload\UploadedFile":private]=> int(1319) ["errorCode":"Swoft\Http\Message\Upload\UploadedFile":private]=> int(0) ["file":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(25) "/tmp/swoole.upfile.f7p2EL" ["clientFilename":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(6) "at.png" ["clientMediaType":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(9) "image/png" ["moved":"Swoft\Http\Message\Upload\UploadedFile":private]=> NULL ["path":"Swoft\Http\Message\Upload\UploadedFile":private]=> NULL } }
若表单中是一个字段数组上传多个文件如 file[]
则返回的是一个二维数组,数组内容依然是 Swoft\Http\Message\Upload\UploadedFile
文件对象,数据结构如下
array(1) { ["file"]=> array(2) { [0]=> object(Swoft\Http\Message\Upload\UploadedFile)#6516 (7) { ["size":"Swoft\Http\Message\Upload\UploadedFile":private]=> int(1319) ["errorCode":"Swoft\Http\Message\Upload\UploadedFile":private]=> int(0) ["file":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(25) "/tmp/swoole.upfile.TVKdOS" ["clientFilename":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(6) "at.png" ["clientMediaType":"Swoft\Http\Message\Upload\UploadedFile":private]=> string(9) "image/png" ["moved":"Swoft\Http\Message\Upload\UploadedFile":private]=> NULL ["path":"Swoft\Http\Message\Upload\UploadedFile":private]=> NULL } ... } }
文件操作方法
moveTo()
将上传的文件移动到新位置。getSize()
获取文件大小,单位byte
。getError()
获取上传文件相关的错误信息,若无错将必须返回UPLOAD_ERR_OK
常量,若又错误将返回UPLOAD_ERR_XXX
相关常量。getClientFilename()
获取文件上传时客户端本地的文件名,不要相信此方法返回的值。客户端可能会发送恶意虚假文件名,意图破坏或破解您的应用程序。getClientMediaType()
获取客户端中文件的MediaType
类型,不要相信此方法返回的值。客户端可能会发送恶意虚假文件名,意图破坏或破解您的应用程序。
其他辅助方法
if ($request->isAjax()) { // Do something } if ($request->isXmlHttpRequest()) { // Do something } if ($request->isGet()) { // Do something } if ($request->isPost()) { // Do something } if ($request->isPut()) { // Do something } if ($request->isDelete()) { // Do something } if ($request->isPatch()) { // Do something } $contentType = $request->getContentType();
(3):Http 响应对象
Swoft 的请求与响应实现于 PSR-7
规范。请求与响应对象存在于每次 HTTP
请求。
- 请求对象
Request
为Swoft\Http\Message\Request
- 响应对象
Response
为Swoft\Http\Message\Response
PSR-7 接口为请求和响应对象提供了这些公共方法:
withProtocolVersion($version)
withHeader($name, $value)
withAddedHeader($name, $value)
withoutHeader($name)
withBody(StreamInterface $body)
PSR-7 接口为响应对象提供了这些方法:
withStatus($code, $reasonPhrase = '')
更多请参考 PSR-7 和 查看 swoft/http-message 中具体的实现类
获取响应对象
- 通过控制器方法参数注入 (
Response $response)
- 通过请求上下文获取
context()->getResponse()
- 通过请求上下文获取
Swoft\Context\Context::mustGet()->getResponse()
_(已废弃)_
示例: 设置响应状态码
$response = \context()->getResponse(); return $response->withStatus(404);
示例: 输出字符串内容响应
return $response->withContent("Hello Swoft2.0");
示例: 输出数组内容响应
$data = ['name'=>'Swoft2.0']; $response->withData($data);
示例: 设置响应头信息
return $response->withHeader("name","Swoft2.0");
(4):
中间件
中间件是用于控制 请求到达
和 响应请求
的整个流程的,通常用于对请求进行过滤验证处理,当你需要对请求或响应作出对应的修改或处理,或想调整请求处理的流程时均可以使用中间件来实现。
定义中间件
只需要实现了 Swoft\Http\Server\Contract\MiddlewareInterface
接口均为一个合法的中间件,其中 process()
方法为该中间件逻辑处理方法。
namespace App\Http\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Http\Server\Contract\MiddlewareInterface; /** * @Bean() */ class ControllerMiddleware implements MiddlewareInterface { /** * Process an incoming server request. * * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface * @inheritdoc */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); return $response; } }
配置全局中间件
当你的自定义中间件需要全局请求应用,则可以考虑将此中间件作为全局中间件去使用,只需在 Bean
配置文件内配置 httpDispatcher
的 middlewares
属性,在数组中加入你的自定义中间件的命名空间地址,相关配置通常在 app/bean.php
内。
return [ ... 'httpDispatcher'=>[ 'middlewares'=>[ AuthMiddleware::class, ApiMiddleware::class ] ] ... ]
通过注解使用
通过 @Middleware
和 @Middlewares
, 可以很方便的配置中间件到当前的 Controller
和 Action
内。
- 当将此注解应用于
Controller
上,则作用域为整个Controller
- 将此注解应用于
Action
上,则作用域仅为当前的Action
@Middleware
用于配置单个中间件@Middlewares
是用于配置一组@Middleware
,按照定义顺序依次执行
namespace App\Http\Controller; use App\Http\Middleware\ApiMiddleware; use App\Http\Middleware\IndexMiddleware; use App\Http\Middleware\ControllerMiddleware; use Swoft\Http\Server\Annotation\Mapping\Controller; use Swoft\Http\Server\Annotation\Mapping\Middleware; use Swoft\Http\Server\Annotation\Mapping\Middlewares; use Swoft\Http\Server\Annotation\Mapping\RequestMapping; /** * @Controller() * @Middlewares({ * @Middleware(ApiMiddleware::class), * @Middleware(ControllerMiddleware::class) * }) */ class MiddlewareController { /** * @RequestMapping() * @Middleware(IndexMiddleware::class) */ public function index(){ return "MiddlewareController"; } }
注意:记得要引入对应的中间件类
应用
示例: 提前拦截请求。
拦截要在
$handler->handle($request)
之前
namespace App\Http\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Context\Context; use Swoft\Http\Server\Contract\MiddlewareInterface; /** * @Bean() */ class SomeMiddleware implements MiddlewareInterface { /** * Process an incoming server request. * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface * @inheritdoc */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $path = $request->getUri()->getPath(); if ($path === '/favicon.ico') { $response = Context::mustGet()->getResponse(); return $response->withStatus(404); } return $handler->handle($request); } }
示例: 跨域设置
namespace App\Http\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Http\Server\Contract\MiddlewareInterface; /** * @Bean() */ class CorsMiddleware implements MiddlewareInterface { /** * Process an incoming server request. * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface * @inheritdoc */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ('OPTIONS' === $request->getMethod()) { $response = Context::mustGet()->getResponse(); return $this->configResponse($response); } $response = $handler->handle($request); return $this->configResponse($response); } private function configResponse(ResponseInterface $response) { return $response ->withHeader('Access-Control-Allow-Origin', 'http://mysite') ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); } }
示例: JWT 登录验证
namespace App\Http\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Context\Context; use Swoft\Http\Server\Contract\MiddlewareInterface; /** * @Bean() */ class AuthMiddleware implements MiddlewareInterface { /** * Process an incoming server request. * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface * @inheritdoc */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // before request handle // 判断token $token = $request->getHeaderLine("token"); $type = \config('jwt.type'); $public = \config('jwt.publicKey'); try { $auth = JWT::decode($token, $public, ['type' => $type]); $request->user = $auth->user; } catch (\Exception $e) { $json = ['code'=>0,'msg'=>'授权失败'] $response = Context::mustGet()->getResponse(); return $response->withData($json); } $response = $handler->handle($request); return $response; // after request handle } }
(5)异常处理
通常我们把异常类放置 app/Exception
,异常类处理器放置 app/Exception/Handler
异常分为两部分。自定义的 Exception
异常类,异常处理类 ExceptionHandler
。
定义异常类
在不同应用场景下,定义不同的异常类,例如需要一个控制器抛异常的类。
namespace App\Exception; class ApiException extends \Exception { }
定义异常处理类
namespace App\Exception\Handler; use App\Exception\ApiException; use Swoft\Error\Annotation\Mapping\ExceptionHandler; use Swoft\Http\Message\Response; use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler; /** * @ExceptionHandler(ApiException::class) */ class ApiExceptionHandler extends AbstractHttpErrorHandler { /** * @param \Throwable $e * @param Response $response * @return Response * @throws \ReflectionException * @throws \Swoft\Bean\Exception\ContainerException */ public function handle(\Throwable $e, Response $response): Response { $data = ['code'=>-1,'msg'=>$e->getMessage()]; return $response->withData($data); } }
@ExceptionHandler 注解
异常处理程序,指定这个处理器要处理当异常,当程序抛出 ExceptionHandler
注解里有的异常时,将会自动执行 handle
方法。
- 指定异常:参数可以是字符串也可以是数组
- 示例: 处理一个异常
@ExceptionHandler(ApiException::class)
- 示例: 处理多个异常
@ExceptionHandler({ApiException::class,ServiceException::class})
- 示例: 处理一个异常