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

更多使用方式 請查看 官方文檔 路由