dragon-code / laravel-route-names
Automatic generation of route names
Fund package maintenance!
Boosty
Yoomoney
Donationalerts
Installs: 8 950
Dependents: 1
Suggesters: 0
Security: 0
Stars: 22
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- fig/http-message-util: ^1.1
- illuminate/routing: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- dragon-code/extended-routes: ^3.2 || ^4.0
- orchestra/testbench: ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.6 || ^10.0
README
Installation
To get the latest version of Laravel Route Names
, simply require the project using Composer:
composer require dragon-code/laravel-route-names
Next, replace Illuminate\Foundation\Application
with DragonCode\LaravelRouteNames\Application
in the bootstrap/app.php
file.
You can now list the routes, for example by calling the php artisan route:list
command or by using
the dragon-code/pretty-routes
package.
Using
This is all. Now you don't have to specify route names. Now the route names will be generated automatically based on the final URL of your project.
All previously specified route names in the application will be ignored.
Compatible with all package solutions for expanding the list of routes.
Also, Laravel Idea support coming soon.
Since route names are generated at the time they are received, we recommend using route caching in production:
php artisan route:cache // or php artisan optimize
Base Routes
app('router')->get('/', [IndexController::class, 'index']); app('router')->post('/', [IndexController::class, 'store']); app('router')->put('/', [IndexController::class, 'update']); app('router')->delete('/', [IndexController::class, 'delete']); app('router')->patch('/', [IndexController::class, 'patch']); app('router')->options('/', [IndexController::class, 'options']); app('router')->get('pages', [PagesController::class, 'index']); app('router')->post('pages', [PagesController::class, 'store']); app('router')->put('pages/{page}', [PagesController::class, 'update']); app('router')->delete('pages/{page}', [PagesController::class, 'delete']); app('router')->patch('pages/{page}', [PagesController::class, 'patch']); app('router')->options('pages/{page}', [PagesController::class, 'options']);
Resource Routes
app('router')->resource('authors/{author}/photos', Author\PhotoController::class);
API Resource Routes
app('router')->apiResource('authors/{author}/photos', Author\PhotoController::class);
List of exclusions
By publishing a configuration file with the artisan command, you can explicitly specify a mask of route names that do not need to be translated:
php artisan vendor:publish --provider="DragonCode\LaravelRouteNames\ServiceProvider"
Exceptions
app('router') ->get('pages', [IndexController::class, 'index']) ->name('my_pages'); return route('my_pages'); // \Symfony\Component\Routing\Exception\RouteNotFoundException // Route [my_pages] not defined. return route('pages.index'); // Returns the result of executing the `IndexController@index` method.
Result
app('router') ->name('pages.') ->prefix('pages') ->group(function () { app('router')->get('/', [Controller::class, 'index']); app('router')->post('/', [Controller::class, 'store']); app('router')->get('{page}', [Controller::class, 'show']); app('router')->delete('{page}', [Controller::class, 'destroy']); });
Before:
GET /pages pages. POST /pages pages. GET /pages/{page} pages. DELETE /pages/{page} pages.
After:
GET /pages pages.index POST /pages pages.store GET /pages/{page} pages.show DELETE /pages/{page} pages.destroy
License
This package is licensed under the MIT License.