secxun / auto-set-routes
自动设置路由.
v1.0.5
2025-11-27 08:30 UTC
Requires
- php: >=7.2
- hyperf/contract: ~2.2.0
- hyperf/di: ~2.2.0
- hyperf/event: ^2.2
- hyperf/http-server: ~2.2.0
- hyperf/json-rpc: ^2.2
- hyperf/rpc-client: ^2.2
- hyperf/utils: ~2.2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/devtool: ~2.2.0
- hyperf/ide-helper: ~2.2.0
- hyperf/testing: ~2.2.0
- mockery/mockery: ^1.0
- swoole/ide-helper: ^4.5
This package is auto-updated.
Last update: 2025-11-27 08:31:08 UTC
README
1.下载
> composer require secxun/auto-set-routes
2. 设置环境标量
# 服务名称
APP_NAME=(英文服务名称)
# NACOS配置
NACOS_HOST=127.0.0.1
NACOS_PORT=8848
NACOS_USERNAME=(null)
NACOS_PASSWORD=(null)
NACOS_GROUP_NAME=
NACOS_NAMESPACE=
3. 确保jsonrpc消费者相关信息已经配置
a.服务配置文件 config/autoload/servers.php
- enable.discovery = true
- drivers.nacos 配置了
<?php
declare(strict_types=1);
return [
// ...
'enable' => [
// 开启服务发现
'discovery' => true,
],
// 服务驱动相关配置
'drivers' => [
'nacos' => [
'host' => env('NACOS_HOST', '127.0.0.1'),
'port' => env('NACOS_PORT', 8848),
'username' => env('NACOS_USERNAME'),
'password' => env('NACOS_PASSWORD'),
'guzzle' => [
'config' => null,
],
'group_name' => env('NACOS_GROUP_NAME', 'DEFAULT_GROUP'),
'namespace_id' => env('NACOS_NAMESPACE', 'public'),
'heartbeat' => 5,
'ephemeral' => true, // 是否注册临时实例
],
],
];
4. 使用教程
1. 使用注解注册路由
需要定义
options.name不需要自动注册时,定义options.in_white_list=true
<?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PutMapping;
/**
* @Controller(prefix="index")
*/
class IndexController
{
/**
* 测试一下.
* @PutMapping(path="all", options={"name": "测试一下"})
*/
public function all()
{
}
}
2. 通过路由文件注册路由
需要定义
options.name不需要自动注册时,定义options.in_white_list=true
<?php
Router::addGroup('/auth', function () {
Router::post('/login', [Controller::class, 'login'], ['name' => '登录', 'in_white_list' => true]);
Router::post('/user-info', [Controller::class, 'userInfo'], ['name' => '用户信息']);
});