Download the PHP package florddev/laravel-auto-routing without Composer
On this page you can find all versions of the php package florddev/laravel-auto-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download florddev/laravel-auto-routing
More information about florddev/laravel-auto-routing
Files in florddev/laravel-auto-routing
Package laravel-auto-routing
Short Description Un package de routage automatique pour Laravel
License MIT
Informations about the package laravel-auto-routing
Laravel Auto Routing
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:
- Add the service provider to your
config/app.php
file:
Basic Usage
In your route file (e.g., routes/web.php
), use the auto
method to automatically register routes for a controller:
In your controller, use attributes to define HTTP methods:
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:
Optional Parameters
The package automatically handles optional parameters:
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:
Controller-Level Route Configuration
You can now use the ControllerRoute
attribute to configure routes at the controller level:
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:
Auto-routing a directory
You can generate routes for all controllers in a specific directory:
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:
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:
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:
After creating the controller, don't forget to register it in your routes file:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.