Un package de routage automatique pour Laravel
florddev/laravel-auto-routing is a Laravel package for un package de routage automatique pour laravel.
It currently has 1 GitHub stars and 13 downloads on Packagist (latest version v1.2.1).
Install it with composer require florddev/laravel-auto-routing.
Discover more Laravel packages by florddev
or browse all Laravel packages to compare alternatives.
Last updated
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.
Install the package via Composer:
composer require florddev/laravel-auto-routing
Add the service provider to your config/app.php file:
'providers' => [
// Other service providers...
Florddev\LaravelAutoRouting\AutoRoutingServiceProvider::class,
],
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) { /* ... */ }
}
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.
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',
]);
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?}
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() { /* ... */ }
}
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.
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']);
});
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.
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);
Contributions are welcome! Please feel free to submit a Pull Request.
This package is open-sourced software licensed under the MIT license.