Hyperf 初体验-路由

  • 2019 年 12 月 18 日
  • 筆記

这次我们来了解下 Hyperf 的路由。主要包括以下内容

  • 如何定义路由
  • 路由的寻址规则

路由定义

路由主要有两种方式来定义

  • 配置文件定义路由
  • 通过注解来定义路由

配置文件定义

config/routes.php 文件中定义所有路由

主要有以下几种方式

<?php  use HyperfHttpServerRouterRouter;    //传入闭包  Router::get('/hello-hyperf', function () {      return 'Hello Hyperf.';  });    // 下面三种方式的任意一种都可以达到同样的效果  Router::get('/hello-hyperf', 'AppControllerIndexController::hello');  Router::get('/hello-hyperf', 'AppControllerIndexController@hello');  Router::get('/hello-hyperf', [AppControllerIndexController::class, 'hello']);

同时 Hyperf 也提供了一些常用的路由

use HyperfHttpServerRouterRouter;    // 注册与方法名一致的 HTTP METHOD 的路由  Router::get($uri, $callback);  Router::post($uri, $callback);  Router::put($uri, $callback);  Router::patch($uri, $callback);  Router::delete($uri, $callback);  Router::head($uri, $callback);    // 注册任意 HTTP METHOD 的路由  Router::addRoute($httpMethod, $uri, $callback);

通过注解来定义路由

注解来定义路由主要有以下两种方式

  • @AutoController 注解
  • @Controller 注解

使用 @AutoController 注解时需 use HyperfHttpServerAnnotationAutoController; 命名空间;

@AutoController 注解
<?php  declare(strict_types=1);    namespace AppController;    use HyperfHttpServerContractRequestInterface;  use HyperfHttpServerAnnotationAutoController;    /**   * @AutoController()   */  class UserController  {      // Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求      public function index(RequestInterface $request)      {          // 从请求中获得 id 参数          $id = $request->input('id', 1);          return (string)$id;      }  }

@Controller 注解

<?php    declare(strict_types=1);    namespace AppController;    use AppControllerController as  AbstractController;  use HyperfHttpServerAnnotationController;  use HyperfHttpServerAnnotationRequestMapping;    /**   * @Controller()   */  class IndexController extends AbstractController  {      /**       * @RequestMapping(path="index", methods="get,post")       */      public function index()      {          $user = $this->request->input('user', 'Hyperf');          $method = $this->request->getMethod();            return [              'method' => $method,              'message' => "Hello {$user}.",          ];      }  }

将会生成 index/index 路由,http://127.0.0.1:9501/indx/index

同时 Hyperf 提供了 5 种 注解,分别是:

使用 @GetMapping 注解时需 use HyperfHttpServerAnnotationGetMapping; 命名空间; 使用 @PostMapping 注解时需 use HyperfHttpServerAnnotationPostMapping; 命名空间; 使用 @PutMapping 注解时需 use HyperfHttpServerAnnotationPutMapping; 命名空间; 使用 @PatchMapping 注解时需 use HyperfHttpServerAnnotationPatchMapping; 命名空间; 使用 @DeleteMapping 注解时需 use HyperfHttpServerAnnotationDeleteMapping; 命名空间;

使用方式如下:

<?php    declare(strict_types=1);    namespace AppController;    use AppControllerController as  AbstractController;  use HyperfHttpServerAnnotationController;  use HyperfHttpServerAnnotationGetMapping;  use HyperfHttpServerAnnotationPostMapping;  use HyperfHttpServerAnnotationRequestMapping;    /**   * @Controller()   */  class IndexController extends AbstractController  {      /**       * @GetMapping(path="index")       * @PostMapping(path="index")       */      public function index()      {          $user = $this->request->input('user', 'Hyperf');          $method = $this->request->getMethod();            return [              'method' => $method,              'message' => "Hello {$user}.",          ];      }  }

路由寻址

Hyperf 的注解依靠 命名空间来实现 URL。例如:namespace AppControllerUser;,则路由就是 /user/**

注解参数

@Controller@AutoController 都提供了 prefixserver 两个参数。

prefix 表示该 Controller 下的所有方法路由的前缀,默认为类名的小写,如 UserControllerprefix 默认为 user,如类内某一方法的 pathindex,则最终路由为 /user/index。 需要注意的是 prefix 并非一直有效,当类内的方法的 path/ 开头时,则表明路径从 URI 头部开始定义,也就意味着会忽略 prefix 的值。

server 表示该路由是定义在哪个 Server 之上的,由于 Hyperf 支持同时启动多个 Server,也就意味着有可能会同时存在多个 HTTP Server,则在定义路由是可以通过 server 参数来进行区分这个路由是为了哪个 Server 定义的,默认为 http

更多使用方式 请查看 官方文档 路由