App_Controller_IndexController.proxy.php 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. declare (strict_types=1);
  3. namespace App\Controller;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Hyperf\HttpServer\Contract\RequestInterface;
  6. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  7. use App\Request\FooRequest;
  8. use App\Request\SceneRequest;
  9. class IndexController extends AbstractController
  10. {
  11. use \Hyperf\Di\Aop\ProxyTrait;
  12. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  13. function __construct(\Hyperf\Logger\LoggerFactory $loggerFactory)
  14. {
  15. if (method_exists(parent::class, '__construct')) {
  16. parent::__construct(...func_get_args());
  17. }
  18. $this->__handlePropertyHandler(__CLASS__);
  19. }
  20. #[Inject]
  21. protected ValidatorFactoryInterface $validationFactory;
  22. public function index(RequestInterface $request)
  23. {
  24. $validator = $this->validationFactory->make($request->all(), ['foo' => 'required', 'bar' => 'required'], ['foo.required' => 'foo必填', 'bar.required' => 'bar必填']);
  25. if ($validator->fails()) {
  26. // Handle exception
  27. $errorMessage = $validator->errors()->first();
  28. var_dump($errorMessage);
  29. }
  30. }
  31. }