florddev/laravel-auto-routing
Composer 安装命令:
composer require florddev/laravel-auto-routing
包简介
Un package de routage automatique pour Laravel
关键字:
README 文档
README
Laravel Auto Routing is a package that simplifies route creation in your Laravel applications using PHP 8 attributes and intuitive naming conventions, while remaining compatible with Laravel's existing routing features.
Features
- Automatic routing based on controller methods
- Use of PHP 8 attributes to define HTTP methods
- Automatic handling of route parameters
- Support for routes with specific HTTP methods or "any"
- Automatic route naming based on controller and method names
- Compatibility with Laravel's existing routing features (middlewares, prefixes, etc.)
- Seamless integration with Laravel's route groups
- Controller-level route configuration using attributes
- Flexible attribute options for both controller and method levels
- Ability to auto route a directory and exclude specific controllers or directories from auto-routing
Requirements
- PHP 8.0 or higher
- Laravel 8.0 or higher
Installation
-
Install the package via Composer:
composer require florddev/laravel-auto-routing
-
Add the service provider to your
config/app.phpfile:'providers' => [ // Other service providers... Florddev\LaravelAutoRouting\AutoRoutingServiceProvider::class, ],
Basic Usage
In your route file (e.g., routes/web.php), use the auto method to automatically register routes for a controller:
Route::auto('/users', \App\Http\Controllers\UserController::class);
In your controller, use attributes to define HTTP methods:
use Florddev\LaravelAutoRouting\Attributes\HttpGet; use Florddev\LaravelAutoRouting\Attributes\HttpPost; use Florddev\LaravelAutoRouting\Attributes\HttpPut; use Florddev\LaravelAutoRouting\Attributes\HttpDelete; class UserController extends Controller { #[HttpGet] public function index() { /* ... */ } #[HttpGet] public function show(int $id) { /* ... */ } #[HttpPost] public function store() { /* ... */ } #[HttpPut] public function update(int $id) { /* ... */ } #[HttpDelete] public function destroy(int $id) { /* ... */ } }
Generated Routes
Here's an example of routes generated for a basic controller:
| Action | Generated Route | HTTP Method | Route Name |
|---|---|---|---|
| index | /users |
GET | users.index |
| show | /users/show/{id} |
GET | users.show |
| store | /users/store |
POST | users.store |
| update | /users/update/{id} |
PUT | users.update |
| destroy | /users/destroy/{id} |
DELETE | users.destroy |
Note: The index method is automatically mapped to the root of the controller's prefix.
Advanced Usage
Adding Options
You can add additional options such as middlewares or prefixes:
Route::auto('/admin/users', \App\Http\Controllers\Admin\UserController::class, [ 'middleware' => ['auth', 'admin'], 'name' => 'admin.users.', 'namespace' => 'Admin', ]);
Optional Parameters
The package automatically handles optional parameters:
use Florddev\LaravelAutoRouting\Attributes\HttpGet; class ApiController extends Controller { #[HttpGet] public function search(string $query, int $page = 1, string $sort = 'desc') { /* ... */ } }
This will generate a route: GET /api/search/{query}/{page?}/{sort?}
Method-Level Route Configuration
You can customize any valid Laravel route option using attribute parameters:
use Florddev\LaravelAutoRouting\Attributes\HttpGet; use Florddev\LaravelAutoRouting\Attributes\HttpPost; class BlogController extends Controller { #[HttpGet(url: "articles", name: "list")] public function index() { /* ... */ } #[HttpGet(url: "article/{slug}", name: "view")] public function show(string $slug) { /* ... */ } #[HttpPost(url: "new-article", middleware: "auth")] public function create() { /* ... */ } }
Controller-Level Route Configuration
You can now use the ControllerRoute attribute to configure routes at the controller level:
use Florddev\LaravelAutoRouting\Attributes\ControllerRoute; #[ControllerRoute(prefix: 'admin', name: 'admin.', middleware: ['auth', 'admin'])] class AdminController extends Controller { // ... controller methods }
This will apply any valid Laravel route option as an attribute parameter in the controller.
Using with Laravel Route Groups
Laravel Auto Routing works seamlessly with Laravel's route groups:
Route::prefix('admin')->middleware('auth')->group(function () { Route::auto('/users', UserController::class); Route::auto('/products', ProductController::class, ['middleware' => 'admin']); });
Auto-routing a directory
You can generate routes for all controllers in a specific directory:
Route::auto('/', app_path('Http/Controllers'));
This will generate routes for all controllers in the /app/Http/Controllers directory.
When auto-routing a directory, you can exclude specific controllers or subdirectories:
Route::auto('/', app_path('Http/Controllers'), [ 'except' => ['/Api', 'ProfileController'] ]);
This will generate routes for all controllers in the /app/Http/Controllers directory, except for ProfileController and any controllers in the Api subdirectory.
Creating Auto-Routed Controllers
You can create a new controller with auto-routing methods using the following Artisan command:
php artisan make:controller UserController --auto
This will generate a new controller with basic CRUD methods already set up with the appropriate auto-routing attributes.
If you want to create a resource controller with all resource methods and auto-routing, use both the --resource and --auto options:
php artisan make:controller UserController --auto --resource
After creating the controller, don't forget to register it in your routes file:
Route::auto('/users', \App\Http\Controllers\UserController::class);
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.
florddev/laravel-auto-routing 适用场景与选型建议
florddev/laravel-auto-routing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「laravel」 「web-development」 「laravel-package」 「php8-attributes」 「backend-development」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 florddev/laravel-auto-routing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 florddev/laravel-auto-routing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 florddev/laravel-auto-routing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
A modern PHP library for converting color codes between RGB, HEX, and HSL color spaces.
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
A router designed for web requests for the Monolith framework
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-24