时间: 2025年06月15日 12:26:20 CST
任务: 修复ThirdParty模块的路由注册问题,参考物品模块的实现方式
ThirdParty模块的路由注册存在以下问题:
#[Resource]注解,而物品模块使用了config/route-attributes.php会自动扫描所有模块的AdminControllers目录参考物品模块的实现,采用路由注解自动注册方式:
为ThirdParty模块的5个控制器添加#[Resource]注解:
ThirdPartyServiceController → #[Resource('thirdparty/services', names: 'dcat.admin.thirdparty.services')]ThirdPartyCredentialController → #[Resource('thirdparty/credentials', names: 'dcat.admin.thirdparty.credentials')]ThirdPartyLogController → #[Resource('thirdparty/logs', names: 'dcat.admin.thirdparty.logs')]ThirdPartyQuotaController → #[Resource('thirdparty/quotas', names: 'dcat.admin.thirdparty.quotas')]ThirdPartyMonitorController → #[Resource('thirdparty/monitors', names: 'dcat.admin.thirdparty.monitors')]移除传统的admin.php路由文件注册方式,保留Webhook和API路由注册:
protected function registerRoutes()
{
// 后台管理路由现在通过路由注解自动注册
// 参考 config/route-attributes.php 配置
// 注册Webhook路由
if (file_exists($webhookRoutes = __DIR__ . '/../Routes/webhook.php')) {
Route::middleware(['api'])
->prefix('thirdParty/webhook')
->name('thirdparty.webhook.')
->group($webhookRoutes);
}
// 注册API路由(如果需要)
if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
Route::middleware(['api'])
->prefix('api/thirdparty')
->group($apiRoutes);
}
}
移除不存在的RestructureExternalManagementMenu命令类引用。
为每个控制器添加必要的use语句和Resource注解:
use Spatie\RouteAttributes\Attributes\Resource;
#[Resource('thirdparty/services', names: 'dcat.admin.thirdparty.services')]
class ThirdPartyServiceController extends AdminController
更新ThirdPartyServiceProvider.php中的路由注册和命令注册方法。
通过浏览器访问验证所有路由正常工作:
/admin/thirdparty/services/admin/thirdparty/credentials/admin/thirdparty/logs/admin/thirdparty/quotas/admin/thirdparty/monitors/admin/thirdparty/reports/overviewconfig/route-attributes.php文件会自动扫描所有模块的AdminControllers目录:
foreach ($Modules as $key => $module) {
if (is_dir($modulePath . '/' . $module . '/AdminControllers')) {
// 后台路由
$return['directories'][app_path('Module/' . $module . '/AdminControllers')] = [
'prefix' => config('admin.route.prefix'),
'middleware' => config('admin.route.middleware'),
'patterns' => ['*Controller.php'],
'not_patterns' => [],
];
}
}
使用Spatie的路由注解包,语法格式:
#[Resource('路由前缀', names: '路由名称前缀')]
这会自动生成标准的RESTful路由:
/admin/thirdparty/services → index/admin/thirdparty/services/create → create/admin/thirdparty/services → store/admin/thirdparty/services/{id} → show/admin/thirdparty/services/{id}/edit → edit/admin/thirdparty/services/{id} → update/admin/thirdparty/services/{id} → destroy修复完成后,所有ThirdParty模块的后台管理页面都能正常访问,菜单显示正确,路由工作正常。
git commit -m "修复ThirdParty模块路由注册问题
- 为所有AdminControllers添加#[Resource]路由注解
- 移除传统的admin.php路由文件注册方式
- 修复ServiceProvider中不存在的命令类引用
- 路由现在通过config/route-attributes.php自动注册
- 所有后台管理页面正常工作:services、credentials、logs、quotas、monitors"
config/route-attributes.php的自动扫描机制很方便,新模块只需要添加注解即可这次修复解决了ThirdParty模块路由注册的问题,使其与项目中其他模块保持一致的路由注册方式。