dannypas00/laravel-dynamic-routes
最新稳定版本:1.0.2
Composer 安装命令:
composer require dannypas00/laravel-dynamic-routes
包简介
A dynamic route service provider that maps route files based on their path
README 文档
README
Laravel dynamic routes is a simple RouteServiceProvider that dynamically resolves routes from route files based on their placement within the filesystem. Route names and prefixes are dynamically allocated to the routes resolved by this provider, and middleware (groups) can be assigned on a directory level.
Installation
composer require dannypas00/laravel-dynamic-routes
Implementation
Implementation is very simple; just change the default RouteServiceProvider import in your
project's App\Providers\RouteServiceProvider from Illuminate\Foundation\Support\Providers\RouteServiceProvider
to DannyPas00\LaravelDynamicRoutes\RouteServiceProvider:
<?php namespace App\Providers; use DannyPas00\LaravelDynamicRoutes\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { public const HOME = '/home'; }
Example
Say we have a directory structure like so:
- MyProject/
- app/
- routes/
- api/
- rest/
- users.php -> index,show
- shops.php -> index,show
- web/
- root.php -> home
- users.php -> index,show
In vanilla laravel, you would need to add code to the RouteServiceProvider to register each of these files individually.
This made managing large projects cumbersome because you would either end up with a couple very big route files (such as
web.php or api.php), or many small ones that all require a separate piece of code in the RouteServiceProvider,
cluttering the provider instead.
With dynamic routing however, all of these route files will be registered without any additional configuration needed.
When this is done, the routes in the route file take on a path associated with the file location.
E.g. the routes registered in the routes/api/rest/users.php file will all be registered as api/rest/users/{route}.
The full output of php artisan route:list would look something like this:
GET|HEAD api/rest/users ............... api.users.index
GET|HEAD api/rest/users/{id} .......... api.users.show
GET|HEAD api/rest/shops ............... api.shops.index
GET|HEAD api/rest/shops/{id} .......... api.shops.show
GET|HEAD users ........................ users.index
GET|HEAD users/{id} ................... users.show
GET|HEAD / ............................ home
Since the dynamic route provider extends the laravel provider, all methods can still be overwritten, so long as the
dynamic provider's boot() or routeRegistration() method is called.
Customizing
The following customization options are available within the route service provider:
Route directory
Setting the ROUTE_DIRECTORY const in the implementing service provider will change the default routes/ directory.
Root file
Setting the ROOT_FILE const in the implementing service provider will change which route files will be treated as "
root" files.
Any root file in any directory will not reflect its name into its prefix or path (see root.php in example).
Flatten directories
Any directory added to the FLATTEN_DIRECTORIES array will be ignored when building paths and prefixes.
Set to just 'web' by default, any file in the routes/web directory will not have "web" in its route or prefix.
Middleware matching
For any directory-wide middleware matching (web and api split for example), the matchMiddleware function can be
overwritten.
It accepts the route's directory as string, and expects a middleware string (or null when not middleware needs to be
set) as a response.
dannypas00/laravel-dynamic-routes 适用场景与选型建议
dannypas00/laravel-dynamic-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 194 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dannypas00/laravel-dynamic-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dannypas00/laravel-dynamic-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 194
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-30