# 路由
herosphp
路由组件 使用的是 fast-route (opens new window)
# http 路由配置
路由暂时没有对应的配置文件, 是通过 @Controller
和 @RequestMap
|@Post
|@Get
组合使用。
tips: 路由一定要设置,否则访问不到, 并且路由值需要是唯一的。
# 注解定义路由
首先在控制器类上添加 @Controller
注解, 接着在类方法添加 @RequestMap
注解, 支持 get|post|put|delete|options
代码示例:
<?php
declare(strict_types=1);
/**
* This file is part of monda-worker.
*
* @contact mondagroup_php@163.com
*/
namespace app\modules\admin\action;
use app\utils\resp\Result;
use herosphp\annotation\Controller;
use herosphp\annotation\Get;
use herosphp\core\BaseController;
use herosphp\core\Config;
#[Controller(name: KeyController::class, desc: '公钥')]
class KeyController extends BaseController
{
#[RequestMap(uri: '/admin/key/publicKey', method:"get", desc: '获取公钥')]
public function publicKey(): Result
{
return Result::ok()->data(['key' => Config::get(name:'rsa', key: 'public_key')]);
}
// 直接使用 Get/Post 注解
#[Post(uri: '/aaa/bbb', desc: '获取私钥')]
public function privateKey(): Result
{
return Result::ok()->data(['key' => Config::get(name:'rsa', key: 'private_key')]);
}
}
访问: http://127.0.0.1:2345/admin/key/publicKey
# @RequestMap 注解参数
- desc: 备注信息,后续将支持一键生成文档。
- uri 定义路由,这是必填的, 需要是唯一,可以是字符串,数组。
- method: 请求方法
get|post|put|delete
, 不设置默认是get
请求, 设置允许多个["get", "post"]
tips: 为了方便使用,衍生而出@GET 和@POST。
# 路由上参数匹配
- 必填参数:
/demo/route/rGet/{id}
- 非必填:
/demo/route/rGet[/{id}]
, 如果不传默认值是false
- 正则匹配:
/demo/route/rGet/{id:\d+}