cakephp-4.x - CakePHP 4 Middleware 返回 withStatus 并中断执行脚本
问题描述
路由.php
$routes->applyMiddleware('MyMiddleware');
应用程序.php
public function routes(\Cake\Routing\RouteBuilder $routes): void {
$options = [
'httponly' => true,
];
$routes->registerMiddleware('csrf', new CsrfProtectionMiddleware($options));
$routes->registerMiddleware('MyMiddleware', new Middleware\MyMiddleware());
parent::routes($routes);
}
src/Middleware/MyMiddleware.php
公共函数过程(ServerRequestInterface $request,RequestHandlerInterface $handler):ResponseInterface { $response = $handler->handle($request); 返回 $response->withStatus(401,'Fail'); }
它正确响应 httpd 401 代码,但脚本继续工作,通过控制器和视图。
我想通过返回 200 以外的 http 代码来停止执行。在 cakephp 4 中不起作用。
有人可以告诉我如何返回 http 401 并破坏脚本。
解决方案
我发现它的唯一形式不使用方法过程,即使在 cakePHP 4 中我也使用了方法 __invoke,我可以得到预期的结果
推荐阅读
- powershell - 用户使用 PowerShell 迁移到 SkypeForBusiness 云
- javascript - 如何修复 Jasmine 中的“TypeError:无法重新定义属性:原点”
- angular - 角度测试无法读取未定义的属性“数据”
- c# - 通过存储过程使用 Dapper
- apache - 如何将带有端口的 URL 重定向到带有上下文的 URL?
- svg - svg 文件中的行未出现在 visio 中
- python-3.x - Python 检查方法或接口是否在运行时之前实现
- angular - Spring boot + Angular应用程序工作
- docker - JHipster/Microservices/Docker 配置中的 MariaDb 访问被拒绝
- apache-kafka - WSO2 SP - 具有 JSON 属性的 Kafka 源