数据返回都需要这样的封装,return $this->success($result);
| class IndexController extends AbstractController |
| { |
| public function index() |
| { |
| $user = $this->request->input('user', '399001'); |
| $method = $this->request->getMethod(); |
| |
| $result = [ |
| 'method' => $method, |
| 'message' => "Hello {$user}.", |
| ]; |
| |
| return $this->success($result); |
| } |
| } |
一般的实现方式是在基类中实现success方法,但会让基类愈加庞大,于是改良使用trait,基类中引入trait,这样也挺好。
突然无聊想到的方法,感觉挺好的:定义返回服务类,使用 __call
在基类中调用服务类的方法。感觉能更方便的解耦耶,贴代码
ResponseService
| <?php |
| |
| |
| namespace App\Service\Utils; |
| |
| |
| use Hyperf\Di\Annotation\Inject; |
| use Hyperf\HttpServer\Contract\ResponseInterface; |
| |
| |
| |
| |
| |
| |
| |
| |
| class ResponseService |
| { |
| |
| |
| |
| private $http_code = 200; |
| |
| |
| |
| |
| |
| |
| private $http_headers = [ |
| 'Author' => 'Colorado', |
| ]; |
| |
| |
| |
| |
| |
| |
| |
| |
| private $business_code = 100000; |
| |
| |
| |
| |
| |
| |
| private $business_msg = 'ok'; |
| |
| |
| |
| |
| |
| private $response; |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setHttpCode(int $code = 200): self{ |
| $this->http_code = $code; |
| |
| return $this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setHttpHeader(string $name, $value): self{ |
| $this->http_headers[$name] = (string)$value; |
| |
| return $this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function success($data, int $business_code = 100000) |
| { |
| $this->business_code = $business_code; |
| |
| return $this->response($data); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function fail(string $error_msg = 'fail', $data = null, int $business_code = 999999) |
| { |
| $this->business_code = $business_code; |
| $this->business_msg = $error_msg; |
| |
| return $this->response($data); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| private function response($data) |
| { |
| $this->response = $this->response->json($this->normalizeData($data))->withStatus($this->http_code); |
| |
| if (! empty($this->http_headers)) { |
| foreach ($this->http_headers as $name => $value) { |
| $this->response = $this->response->withHeader($name, $value); |
| } |
| } |
| |
| return $this->response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| private function normalizeData($data): array{ |
| return [ |
| 'code' => $this->business_code, |
| 'data' => $data, |
| 'message' => $this->business_msg, |
| 'timestamp' => time(), |
| ]; |
| } |
| } |
基类AbstractController
| <?php |
| |
| declare(strict_types=1); |
| |
| |
| |
| |
| |
| |
| |
| |
| namespace App\Controller; |
| |
| use App\Service\Utils\ResponseService; |
| use Hyperf\Di\Annotation\Inject; |
| use Hyperf\HttpServer\Contract\RequestInterface; |
| use Hyperf\HttpServer\Contract\ResponseInterface; |
| use Psr\Container\ContainerInterface; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| abstract class AbstractController |
| { |
| |
| |
| |
| |
| protected $container; |
| |
| |
| |
| |
| |
| protected $request; |
| |
| |
| |
| |
| |
| protected $response; |
| |
| |
| |
| |
| |
| |
| |
| public function __call($name, $arguments) |
| { |
| if (method_exists(ResponseService::class, $name)) { |
| return make(ResponseService::class)->{$name}(...$arguments); |
| } |
| } |
| } |
使用demo
控制器中
| class IndexController extends AbstractController |
| { |
| public function index() |
| { |
| $user = $this->request->input('user', '399001'); |
| $method = $this->request->getMethod(); |
| |
| $result = [ |
| 'method' => $method, |
| 'message' => "Hello {$user}.", |
| ]; |
| |
| |
| return $this->success($result); |
| |
| return $this->fail('失败啦'); |
| |
| return $this->setHttpCode(201)->success($result); |
| return $this->setHttpCode(500)->fail('服务器挂了'); |
| |
| return $this->setHttpHeader('server', 'hyperf-server')->success($result); |
| } |
| } |
其他地方,如ExceptionHandler
使用make该类即可调用类的方法
| class ValidationExceptionHandler extends ExceptionHandler |
| { |
| public function handle(Throwable $throwable, ResponseInterface $response) |
| { |
| $this->stopPropagation(); |
| $body = $throwable->validator->errors()->first(); |
| |
| return make(ResponseService::class)->fail($body); |
| } |
| |
| public function isValid(Throwable $throwable): bool{ |
| return $throwable instanceof ValidationException; |
| } |
| } |
| |