####必须使用@Controller(prefix="index")定义前缀 `RequestMapping`定义路由 |参数|示例|说明| |----|----|----| |path|test|路径| |methods|get,post|允许的请求方式,多个方式逗号分隔| >路由地址为`/index/test` ``` <?php declare(strict_types=1); namespace App\Controller\User; use App\Controller\AbstractController; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; /** * @Controller(prefix="index") */ class IndexController extends AbstractController { /** * @RequestMapping(path="test",methods={"get","post"}) */ public function index() { $user = $this->request->input('user', 'Hyperf'); return [ 'method' => $this->request->getMethod(), 'message' => "Hello6 {$user}.", ]; } } ``` `PostMapping`定义路由,只允许`post`方式请求 |参数|示例|说明| |----|----|----| |path|index|路径| >路径为`/index/test` ``` <?php declare(strict_types=1); namespace App\Controller\User; use App\Controller\AbstractController; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\PostMapping; /** * @Controller(prefix="index") */ class IndexController extends AbstractController { /** * @PostMapping(path="test") */ public function index() { $user = $this->request->input('user', 'Hyperf'); return [ 'method' => $this->request->getMethod(), 'message' => "Hello5 {$user}.", ]; } } ``` ####无论哪种方式,如果想让`index`方法不受`Controller`注解的约束,那么`path`的值最前方必须增加`/` 非特殊说明,本文版权归秦官人所有,转载请注明出处. 0 本文标题: hyperf Controller注解定义路由的几种方式 延伸阅读 上一篇: hyperf AutoController路由定义 下一篇: hyperf 协程上下文 发表评论 说点什么吧 | 0评论 最新 最早 还没有评论,快来抢沙发吧!