* @link https://github.com/gzydong/hyperf-chat */ namespace App\Controller; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\ResponseInterface; use Psr\Container\ContainerInterface; use Hyperf\Validation\Contract\ValidatorFactoryInterface; use App\Exception\ValidateException; use App\Constant\ResponseCode; abstract class AbstractController { /** * @Inject * @var ContainerInterface */ protected $container; /** * @Inject * @var RequestInterface */ protected $request; /** * @Inject * @var ResponseInterface */ protected $response; /** * 自定义控制器验证器 * * @param mixed ...$arg */ protected function validate(...$arg) { $validator = di()->get(ValidatorFactoryInterface::class)->make(...$arg); if ($validator->fails()) { throw new ValidateException($validator->errors()->first(), ResponseCode::VALIDATION_ERROR); } } }