title: Annotation controller meta:

  • name: description content: Easyswoole provides lightweight annotation controller support
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole|Annotation controller|Annotation parameter verification

Annotation controller

Easyswoole provides lightweight annotation controller support. The annotation controller is logically consistent with the normal controller logic when no annotations are added.

::: warning This function is in the grayscale test. Students who want to experience can add the require configuration item "easyswoole/http": "master-dev as 1.3.0" to composer and execute composer up to install. :::

Examples are as follows

  1. use EasySwoole\Http\Annotation\Method;
  2. class Test extends \EasySwoole\Http\AbstractInterface\AnnotationController
  3. {
  4. /**
  5. * @\EasySwoole\Http\Annotation\Context(key="MYSQL")
  6. */
  7. public $mysql;
  8. /**
  9. * @var
  10. * @\EasySwoole\Http\Annotation\DI(key="IOC")
  11. */
  12. public $IOC;
  13. function index()
  14. {
  15. // TODO: Implement index() method.
  16. }
  17. /**
  18. * @Method(allow={GET,POST})
  19. * @\EasySwoole\Http\Annotation\Param(name="test",from={POST})
  20. * @\EasySwoole\Http\Annotation\Param(name="msg",alias="Message field",lengthMax="20|The message is too long",required="Message cannot be empty")
  21. * @\EasySwoole\Http\Annotation\Param(name="type",inArray="{1,2,3,4}")
  22. */
  23. function fuck($test,$msg)
  24. {
  25. var_dump($test,$msg);
  26. }
  27. protected function onException(\Throwable $throwable): void
  28. {
  29. if($throwable instanceof \EasySwoole\Http\Exception\ParamAnnotationValidateError){
  30. var_dump($throwable->getValidate()->getError()->getErrorRuleMsg());
  31. }else{
  32. var_dump($throwable->getMessage());
  33. }
  34. }
  35. }

::: warning The verification method supported by Param annotation is consistent with the parameter checker provided by Easyswoole. Please refer to annotation for the annotation format. :::