这次我们来了解下 Hyperf 的路由。主要包括以下内容
路由主要有两种方式来定义
在 config/routes.php 文件中定义所有路由
主要有以下几种方式
<?php
use Hyperf\HttpServer\Router\Router;
//传入闭包
Router::get('/hello-hyperf', function () {
return 'Hello Hyperf.';
});
// 下面三种方式的任意一种都可以达到同样的效果
Router::get('/hello-hyperf', 'App\Controller\IndexController::hello');
Router::get('/hello-hyperf', 'App\Controller\IndexController@hello');
Router::get('/hello-hyperf', [App\Controller\IndexController::class, 'hello']);同时 Hyperf 也提供了一些常用的路由
use Hyperf\HttpServer\Router\Router;
// 注册与方法名一致的 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 Hyperf\HttpServer\Annotation\AutoController;命名空间;
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class UserController
{
// Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
public function index(RequestInterface $request)
{
// 从请求中获得 id 参数
$id = $request->input('id', 1);
return (string)$id;
}
}<?php
declare(strict_types=1);
namespace App\Controller;
use App\Controller\Controller as AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
/**
* @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 Hyperf\HttpServer\Annotation\GetMapping;命名空间; 使用@PostMapping 注解时需 use Hyperf\HttpServer\Annotation\PostMapping;命名空间; 使用@PutMapping 注解时需 use Hyperf\HttpServer\Annotation\PutMapping;命名空间; 使用@PatchMapping注解时需use Hyperf\HttpServer\Annotation\PatchMapping;命名空间; 使用@DeleteMapping注解时需use Hyperf\HttpServer\Annotation\DeleteMapping;命名空间;
使用方式如下:
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Controller\Controller as AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
/**
* @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 App\Controller\User;,则路由就是/user/**
@Controller 和 @AutoController 都提供了 prefix 和 server 两个参数。
prefix 表示该 Controller 下的所有方法路由的前缀,默认为类名的小写,如 UserController 则 prefix 默认为 user,如类内某一方法的 path 为 index,则最终路由为 /user/index。 需要注意的是 prefix 并非一直有效,当类内的方法的 path 以 / 开头时,则表明路径从 URI 头部开始定义,也就意味着会忽略 prefix 的值。
server 表示该路由是定义在哪个 Server 之上的,由于 Hyperf 支持同时启动多个 Server,也就意味着有可能会同时存在多个 HTTP Server,则在定义路由是可以通过 server 参数来进行区分这个路由是为了哪个 Server 定义的,默认为 http。
更多使用方式 请查看 官方文档 路由