方式一 :在路由文件里为某个控制器注册路由
通过配置文件定义路由,在 hyperf-skeleton 骨架下,默认在 config/routes.php 文件内完成所有的路由定义
<?php
use Hyperf\HttpServer\Router\Router;
# 第一种方式: http://127.0.0.1:9501/test/1
Router::get('/test', 'AppControllerIndexController@test');
# 第二种方式: http://127.0.0.1:9501/test/
Router::addRoute(['GET'], '/test', 'AppControllerIndexController@test');
# 第三种方式:匿名函数 http://127.0.0.1:9501/test2
Router::get('/test2', function () {
return 'hello world';
});
-------------------------------------------------------
# 组的方式定义路由 http://127.0.0.1:9501/api/test/1
Router::addGroup('/api', function () {
Router::get('/test/{id}', 'AppControllerIndexController@test');
});
# 加入中间件
Router::get('/test/{id}', 'AppControllerIndexController@test', [
"middleware" => [ApiSignMiddleware::class]
]);方式二:通过注解的方式
#[AutoController] 注解
使用 #[AutoController] 时则 Hyperf 会自动解析所在类的所有 public 方法并提供 GET 和 POST 两种请求方式。
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
#[AutoController(prefix: 'client')]
class UserController
{
// Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
public function index(RequestInterface $request)
{
// 从请求中获得 id 参数
$id = $request->input('id', 1);
return (string)$id;
}
public function list()
{
return __METHOD__;
}
}
访问方式:
如果没有加prefix参数的话 url是http://localhost:9501/client/index
如果定义了prefix参数 url就是 http://localhost:9501/user/index#[Controller] 注解
使用 #[Controller] 注解用于表明当前类为一个 Controller 类,同时需配合 #[RequestMapping] 注解来对请求方法和请求路径进行更详细的定义。
我们也提供了多种快速便捷的 Mapping 注解,如 #[GetMapping]、#[PostMapping]、#[PutMapping]、#[PatchMapping]、#[DeleteMapping] 5 种便捷的注解用于表明允许不同的请求方法。
使用 @Controller 注解时需 use Hyperf\HttpServer\Annotation\Controller; 命名空间;
使用 @RequestMapping 注解时需 use Hyperf\HttpServer\Annotation\RequestMapping; 命名空间;
使用 @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; 命名空间;
RequestMapping
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
#[Controller(prefix: 'user')]
class ClientController extends AbstractController
{
#[RequestMapping(path: 'index', methods: 'get')]
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
}
访问方式:
http://127.0.0.1:9501/user/indexGetMapping
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
#[Controller(prefix: 'user')]
class ClientController extends AbstractController
{
#[GetMapping(path: 'index_aaa')]
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
}
访问方式:
http://127.0.0.1:9501/user/index_aaa
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn