Files
hyperf_service/app/Middleware/CoreMiddleware.php
2024-11-11 13:06:26 +08:00

43 lines
1.3 KiB
PHP

<?php
namespace App\Middleware;
use App\Lib\AdminReturn;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{
/**
* @var AdminReturn
*/
#[Inject]
protected AdminReturn $return;
/**
* 404重写
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function handleNotFound(ServerRequestInterface $request): ResponseInterface
{
return $this->response()->withHeader("Content-Type", "application/json")
->withStatus(200)
->withBody(new SwooleStream(json_encode($this->return->error('路由不存在'), JSON_UNESCAPED_UNICODE)));
}
/**
* 405重写
* @param array $methods
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function handleMethodNotAllowed(array $methods, ServerRequestInterface $request): ResponseInterface
{
return $this->response()->withHeader("Content-Type", "application/json")
->withStatus(200)
->withBody(new SwooleStream(json_encode($this->return->error('路由请求方法不正确'), JSON_UNESCAPED_UNICODE)));
}
}