ricventu / laravel-route-maze
A quick and easy way to create the routes is to take advantage of the convention over configuration
Fund package maintenance!
Ko Fi
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
README
A quick and easy way to auto generate routes, is to take advantage of the convention over configuration and PHP attributes.
This means that routes are automatically generated based on the directory structure of the controllers and the methods attributes.
In this way, you don't have to manually write the routes in the web.php or api.php file, but just follow some rules of file naming and organization.
Route groups are base on subdirectories of the controllers.
Let's see how to do it with a practical example.
Controller: App/Http/Controllers/SomeCategory/ProductsController.php
class ProductsController { // index and __invoke defaults to GET public function index() {...} #[Get] public function show($id) {...} #[Post] public function store(Request $request) {...} #[Patch] public function update($id, Request $request) {...} #[Delete] public function destroy($id) {...} }
To get routes simpli add the the following line to routes/web.php
Route::maze(app_path('Http/Controllers'), 'App\\Http\\Controllers');
The generated routes are:
Route::get('/some-category/products', 'SomeCategory\ProductsController@index')->name('some-category.products'); Route::get('/some-category/products/show/{id}', 'SomeCategory\ProductsController@show')->name('some-category.products.show'); Route::post('/some-category/products/store', 'SomeCategory\ProductsController@store')->name('some-category.products.store'); Route::Patch('/some-category/products/update/{id}', 'SomeCategory\ProductsController@update')->name('some-category.products.update'); Route::delete('/some-category/products/destroy/{id}', 'SomeCategory\ProductsController@destroy')->name('some-category.products.destroy');
Parameters in path
Parameters can be specified in the path naming the directory with _param-name_
.
Http/Controllers/_param1_/ItemsController.php
class ItemsController { #[Get] public function get($id) {...} }
becomes
Route::get('/{param1}/items/get/{id}', 'ItemsController@get')->name('items.get');
Middleware
You can specify middleware group by adding a file named middleware.php
in the controller directory.
return [ 'auth', ];
Naming conventions
Uri and route name are composed of directories name, first part of the controller name (before Controller
) and method name, all in kebab-case.
examples:
Disable discover for a Controller
To disable route discover for a specified crontroller, add static method mazeDisabled
that returns true
In path configuration
If in the path is present a file named maze.php
, it will be used to configure the route group.
return [
];
## Installation You can install the package via composer: ```bash composer require ricventu/laravel-route-maze
Usage
Route::maze(app_path('Http/Controllers'), 'App\\Http\\Controllers');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Performance note
In production deployment, it's recommended to cache route discovering using Laravel route:cache
built in command
Credits
License
The MIT License (MIT). Please see License File for more information.