├── LICENSE ├── README.md ├── composer.json ├── config └── route.php └── src ├── Application.php ├── Helpers ├── Action.php └── Name.php ├── Providers └── RoutingServiceProvider.php ├── Routing ├── Route.php └── Router.php └── ServiceProvider.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022-2025 Andrey Helldar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automatic Route Names for Laravel 2 | 3 | ![the dragon code route names](https://preview.dragon-code.pro/the-dragon-code/auto-route-names.svg?brand=laravel) 4 | 5 | [![Stable Version][badge_stable]][link_packagist] 6 | [![Total Downloads][badge_downloads]][link_packagist] 7 | [![Github Workflow Status][badge_build]][link_build] 8 | [![License][badge_license]][link_license] 9 | 10 | ## Installation 11 | 12 | To get the latest version of `Laravel Route Names`, simply require the project 13 | using [Composer](https://getcomposer.org): 14 | 15 | ```bash 16 | composer require dragon-code/laravel-route-names 17 | ``` 18 | 19 | > [!WARNING] 20 | > 21 | > For naming to work correctly, you need to replace the application class otherwise the code will be loaded too late. 22 | > 23 | > To do this, replace `Illuminate\Foundation\Application` with `DragonCode\LaravelRouteNames\Application` in the 24 | `bootstrap/app.php` file. 25 | 26 | You can now list the routes, for example by calling the `php artisan route:list` command or by using 27 | the [`dragon-code/pretty-routes`](https://github.com/TheDragonCode/pretty-routes) package. 28 | 29 | ## Using 30 | 31 | This is all. Now you don't have to specify route names. Now the route names will be generated automatically based on the 32 | final URL of your project. 33 | 34 | > All previously specified route names in the application will be ignored. 35 | > 36 | > Compatible with all package solutions for expanding the list of routes. 37 | 38 | Since route names are generated at the time they are received, we recommend using route caching in production: 39 | 40 | ```bash 41 | php artisan route:cache 42 | 43 | // or 44 | 45 | php artisan optimize 46 | ``` 47 | 48 | ### Base Routes 49 | 50 | ```php 51 | app('router')->get('/', [IndexController::class, 'index']); 52 | app('router')->post('/', [IndexController::class, 'store']); 53 | app('router')->put('/', [IndexController::class, 'update']); 54 | app('router')->delete('/', [IndexController::class, 'delete']); 55 | app('router')->patch('/', [IndexController::class, 'patch']); 56 | app('router')->options('/', [IndexController::class, 'options']); 57 | 58 | app('router')->get('{some}', [IndexController::class, 'index']); 59 | app('router')->post('{some}', [IndexController::class, 'store']); 60 | app('router')->put('{some}', [IndexController::class, 'update']); 61 | app('router')->delete('{some}', [IndexController::class, 'delete']); 62 | app('router')->patch('{some}', [IndexController::class, 'patch']); 63 | app('router')->options('{some}', [IndexController::class, 'options']); 64 | 65 | app('router')->get('pages', [PagesController::class, 'index']); 66 | app('router')->post('pages', [PagesController::class, 'store']); 67 | app('router')->put('pages/{page}', [PagesController::class, 'update']); 68 | app('router')->delete('pages/{page}', [PagesController::class, 'delete']); 69 | app('router')->patch('pages/{page}', [PagesController::class, 'patch']); 70 | app('router')->options('pages/{page}', [PagesController::class, 'options']); 71 | ``` 72 | 73 | | Method | Url | Name | Helper | 74 | |-----------|--------------|-----------------|--------------------------| 75 | | GET, HEAD | `/` | `main.index` | `route('main.index')` | 76 | | POST | `/` | `main.store` | `route('main.store')` | 77 | | PUT | `/` | `main.update` | `route('main.update')` | 78 | | DELETE | `/` | `main.destroy` | `route('main.destroy')` | 79 | | PATCH | `/` | `main.patch` | `route('main.patch')` | 80 | | OPTIONS | `/` | `main.options` | `route('main.options')` | 81 | | GET, HEAD | `{some}` | `some.index` | `route('some.index')` | 82 | | POST | `{some}` | `some.store` | `route('some.store')` | 83 | | PUT | `{some}` | `some.update` | `route('some.update')` | 84 | | DELETE | `{some}` | `some.destroy` | `route('some.destroy')` | 85 | | PATCH | `{some}` | `some.patch` | `route('some.patch')` | 86 | | OPTIONS | `{some}` | `some.options` | `route('some.options')` | 87 | | GET, HEAD | `/pages` | `pages.index` | `route('pages.index')` | 88 | | POST | `/pages` | `pages.store` | `route('pages.store')` | 89 | | PUT | `/pages/123` | `pages.update` | `route('pages.update')` | 90 | | DELETE | `/pages/123` | `pages.destroy` | `route('pages.destroy')` | 91 | | PATCH | `/pages/123` | `pages.patch` | `route('pages.patch')` | 92 | | OPTIONS | `/pages/123` | `pages.options` | `route('pages.options')` | 93 | 94 | ### Resource Routes 95 | 96 | ```php 97 | app('router')->resource('authors/{author}/photos', Author\PhotoController::class); 98 | ``` 99 | 100 | | Method | Url | Name | Helper | 101 | |-----------|------------------------------------|--------------------------|-----------------------------------| 102 | | GET, HEAD | `/authors/123/photos` | `authors.photos.index` | `route('authors.photos.index')` | 103 | | GET, HEAD | `/authors/123/photos/create` | `authors.photos.create` | `route('authors.photos.create')` | 104 | | POST | `/authors/123/photos` | `authors.photos.store` | `route('authors.photos.store')` | 105 | | GET | `/authors/123/photos/{photo}` | `authors.photos.show` | `route('authors.photos.show')` | 106 | | GET | `/authors/123/photos/{photo}/edit` | `authors.photos.edit` | `route('authors.photos.edit')` | 107 | | PUT | `/authors/123/photos/{photo}` | `authors.photos.update` | `route('authors.photos.update')` | 108 | | PATCH | `/authors/123/photos/{photo}` | `authors.photos.patch` | `route('authors.photos.patch')` | 109 | | DELETE | `/authors/123/photos/{photo}` | `authors.photos.destroy` | `route('authors.photos.destroy')` | 110 | 111 | ### API Resource Routes 112 | 113 | ```php 114 | app('router')->apiResource('authors/{author}/photos', Author\PhotoController::class); 115 | ``` 116 | 117 | | Method | Url | Name | Helper | 118 | |-----------|-------------------------------|--------------------------|-----------------------------------| 119 | | GET, HEAD | `/authors/123/photos` | `authors.photos.index` | `route('authors.photos.index')` | 120 | | POST | `/authors/123/photos` | `authors.photos.store` | `route('authors.photos.store')` | 121 | | GET | `/authors/123/photos/{photo}` | `authors.photos.show` | `route('authors.photos.show')` | 122 | | PUT | `/authors/123/photos/{photo}` | `authors.photos.update` | `route('authors.photos.update')` | 123 | | PATCH | `/authors/123/photos/{photo}` | `authors.photos.patch` | `route('authors.photos.patch')` | 124 | | DELETE | `/authors/123/photos/{photo}` | `authors.photos.destroy` | `route('authors.photos.destroy')` | 125 | 126 | ### List of exclusions 127 | 128 | By publishing a configuration file with the artisan command, you can explicitly specify a mask of route names that do 129 | not need to be translated: 130 | 131 | ```bash 132 | php artisan vendor:publish --provider="DragonCode\LaravelRouteNames\ServiceProvider" 133 | ``` 134 | 135 | ### Exceptions 136 | 137 | ```php 138 | app('router') 139 | ->get('pages', [IndexController::class, 'index']) 140 | ->name('my_pages'); 141 | 142 | return route('my_pages'); 143 | // \Symfony\Component\Routing\Exception\RouteNotFoundException 144 | // Route [my_pages] not defined. 145 | 146 | return route('pages.index'); 147 | // Returns the result of executing the `IndexController@index` method. 148 | ``` 149 | 150 | ## Result 151 | 152 | ```php 153 | app('router') 154 | ->name('pages.') 155 | ->prefix('pages') 156 | ->group(function () { 157 | app('router')->get('/', [Controller::class, 'index']); 158 | app('router')->post('/', [Controller::class, 'store']); 159 | app('router')->get('{page}', [Controller::class, 'show']); 160 | app('router')->delete('{page}', [Controller::class, 'destroy']); 161 | }); 162 | ``` 163 | 164 | **Before:** 165 | 166 | ```bash 167 | GET /pages pages. 168 | POST /pages pages. 169 | GET /pages/{page} pages. 170 | DELETE /pages/{page} pages. 171 | ``` 172 | 173 | **After:** 174 | 175 | ```bash 176 | GET /pages pages.index 177 | POST /pages pages.store 178 | GET /pages/{page} pages.show 179 | DELETE /pages/{page} pages.destroy 180 | ``` 181 | 182 | ## License 183 | 184 | This package is licensed under the [MIT License](LICENSE). 185 | 186 | 187 | [badge_build]: https://img.shields.io/github/actions/workflow/status/TheDragonCode/laravel-route-names/phpunit.yml?style=flat-square 188 | 189 | [badge_downloads]: https://img.shields.io/packagist/dt/dragon-code/laravel-route-names.svg?style=flat-square 190 | 191 | [badge_license]: https://img.shields.io/packagist/l/dragon-code/laravel-route-names.svg?style=flat-square 192 | 193 | [badge_stable]: https://img.shields.io/github/v/release/TheDragonCode/laravel-route-names?label=stable&style=flat-square 194 | 195 | [link_build]: https://github.com/TheDragonCode/laravel-route-names/actions 196 | 197 | [link_license]: LICENSE 198 | 199 | [link_packagist]: https://packagist.org/packages/dragon-code/laravel-route-names 200 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dragon-code/laravel-route-names", 3 | "description": "Automatic generation of route names", 4 | "license": "MIT", 5 | "keywords": [ 6 | "laravel", 7 | "dragon-code", 8 | "routes", 9 | "names", 10 | "naming", 11 | "routing" 12 | ], 13 | "authors": [ 14 | { 15 | "name": "Andrey Helldar", 16 | "email": "helldar@dragon-code.pro", 17 | "homepage": "https://dragon-code.pro" 18 | } 19 | ], 20 | "support": { 21 | "issues": "https://github.com/TheDragonCode/laravel-route-names/issues", 22 | "source": "https://github.com/TheDragonCode/laravel-route-names" 23 | }, 24 | "funding": [ 25 | { 26 | "type": "boosty", 27 | "url": "https://boosty.to/dragon-code" 28 | }, 29 | { 30 | "type": "yoomoney", 31 | "url": "https://yoomoney.ru/to/410012608840929" 32 | } 33 | ], 34 | "require": { 35 | "php": "^8.2", 36 | "illuminate/routing": "^11.0 || ^12.0" 37 | }, 38 | "require-dev": { 39 | "orchestra/testbench": "^9.0 || ^10.0", 40 | "pestphp/pest": "^3.8", 41 | "pestphp/pest-plugin-laravel": "^3.2", 42 | "pestphp/pest-plugin-type-coverage": "^3.5" 43 | }, 44 | "minimum-stability": "stable", 45 | "prefer-stable": true, 46 | "autoload": { 47 | "psr-4": { 48 | "DragonCode\\LaravelRouteNames\\": "src/" 49 | } 50 | }, 51 | "autoload-dev": { 52 | "psr-4": { 53 | "Tests\\": "tests/", 54 | "Workbench\\App\\": "workbench/app/" 55 | } 56 | }, 57 | "config": { 58 | "allow-plugins": { 59 | "composer/package-versions-deprecated": true, 60 | "dragon-code/codestyler": true, 61 | "ergebnis/composer-normalize": true, 62 | "friendsofphp/php-cs-fixer": true, 63 | "pestphp/pest-plugin": true, 64 | "symfony/thanks": true 65 | }, 66 | "preferred-install": "dist", 67 | "sort-packages": true 68 | }, 69 | "extra": { 70 | "laravel": { 71 | "providers": [ 72 | "DragonCode\\LaravelRouteNames\\ServiceProvider" 73 | ] 74 | } 75 | }, 76 | "scripts": { 77 | "post-autoload-dump": [ 78 | "@clear", 79 | "@prepare" 80 | ], 81 | "build": "@php vendor/bin/testbench workbench:build --ansi", 82 | "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", 83 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 84 | "test": "@php vendor/bin/pest", 85 | "test-coverage": "@test --type-coverage --compact" 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /config/route.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | return [ 19 | /* 20 | |-------------------------------------------------------------------------- 21 | | Names 22 | |-------------------------------------------------------------------------- 23 | | 24 | | This option determines the handling of route names. 25 | | 26 | */ 27 | 28 | 'names' => [ 29 | /* 30 | |-------------------------------------------------------------------------- 31 | | Exclude Names 32 | |-------------------------------------------------------------------------- 33 | | 34 | | This option specifies the names of the routes that will be excluded 35 | | from the conversion. 36 | | 37 | */ 38 | 39 | 'exclude' => [ 40 | '__clockwork*', 41 | '_debugbar*', 42 | '_ignition*', 43 | 'horizon*', 44 | 'pretty-routes*', 45 | 'sanctum*', 46 | 'telescope*', 47 | ], 48 | 49 | /* 50 | |-------------------------------------------------------------------------- 51 | | Extender 52 | |-------------------------------------------------------------------------- 53 | | 54 | | This option specifies the callback that will be used to extend route names. 55 | | 56 | */ 57 | 58 | 'extender' => null, 59 | ], 60 | ]; 61 | -------------------------------------------------------------------------------- /src/Application.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames; 19 | 20 | use DragonCode\LaravelRouteNames\Providers\RoutingServiceProvider; 21 | use Illuminate\Events\EventServiceProvider; 22 | use Illuminate\Foundation\Application as BaseApplication; 23 | use Illuminate\Log\Context\ContextServiceProvider; 24 | use Illuminate\Log\LogServiceProvider; 25 | 26 | class Application extends BaseApplication 27 | { 28 | protected function registerBaseServiceProviders(): void 29 | { 30 | $this->register(new EventServiceProvider($this)); 31 | $this->register(new LogServiceProvider($this)); 32 | 33 | if (class_exists(ContextServiceProvider::class)) { 34 | $this->register(new ContextServiceProvider($this)); 35 | } 36 | 37 | $this->register(new RoutingServiceProvider($this)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Helpers/Action.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames\Helpers; 19 | 20 | use Illuminate\Support\Str; 21 | use Symfony\Component\HttpFoundation\Request; 22 | 23 | use function in_array; 24 | 25 | class Action 26 | { 27 | protected array $aliases = [ 28 | 'index' => [Request::METHOD_GET, Request::METHOD_HEAD], 29 | 'store' => [Request::METHOD_POST], 30 | 'update' => [Request::METHOD_PUT], 31 | 'destroy' => [Request::METHOD_DELETE], 32 | 'patch' => [Request::METHOD_PATCH], 33 | 'options' => [Request::METHOD_OPTIONS], 34 | ]; 35 | 36 | protected array $collision = [ 37 | 'create' => [Request::METHOD_GET, Request::METHOD_HEAD], 38 | 'show' => [Request::METHOD_GET, Request::METHOD_HEAD], 39 | 'edit' => [Request::METHOD_GET, Request::METHOD_HEAD], 40 | 'destroy' => [Request::METHOD_DELETE], 41 | 'delete' => [Request::METHOD_DELETE], 42 | 'trashed' => [Request::METHOD_GET, Request::METHOD_HEAD], 43 | 'restore' => [Request::METHOD_POST], 44 | ]; 45 | 46 | protected array $show = [ 47 | Request::METHOD_GET, 48 | Request::METHOD_HEAD, 49 | ]; 50 | 51 | protected string $default = 'index'; 52 | 53 | public function get(array $methods, string $uri): string 54 | { 55 | return $this->show($methods, $uri) 56 | ?? $this->collision($methods, $uri) 57 | ?? $this->alias($methods) 58 | ?? $this->default; 59 | } 60 | 61 | protected function collision(array $methods, string $uri): ?string 62 | { 63 | foreach ($this->collision as $alias => $httpMethods) { 64 | if ($this->hasMethods($methods, $httpMethods) && $this->hasAlias($uri, $alias)) { 65 | return $alias; 66 | } 67 | } 68 | 69 | return null; 70 | } 71 | 72 | protected function alias(array $methods): ?string 73 | { 74 | foreach ($this->aliases as $method => $httpMethods) { 75 | if ($this->hasMethods($methods, $httpMethods)) { 76 | return $method; 77 | } 78 | } 79 | 80 | return null; 81 | } 82 | 83 | protected function show(array $methods, string $uri): ?string 84 | { 85 | if ($this->hasMethods($methods, $this->show) && Str::of($uri)->rtrim(' /')->endsWith('}')) { 86 | return 'show'; 87 | } 88 | 89 | return null; 90 | } 91 | 92 | protected function hasMethods(array $route, array $aliases): bool 93 | { 94 | foreach ($route as $value) { 95 | if (in_array($value, $aliases, true)) { 96 | return true; 97 | } 98 | } 99 | 100 | return false; 101 | } 102 | 103 | protected function hasAlias(string $uri, string $alias): bool 104 | { 105 | return Str::of($uri)->lower()->endsWith($alias); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Helpers/Name.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames\Helpers; 19 | 20 | use Illuminate\Support\Collection; 21 | use Illuminate\Support\Str; 22 | 23 | class Name 24 | { 25 | protected string $default = 'main'; 26 | 27 | public function __construct( 28 | protected Action $action 29 | ) {} 30 | 31 | public function get(array $methods, string $uri): string 32 | { 33 | $resolved = $this->resolve($uri); 34 | $suffix = $this->getMethodSuffix($methods, $uri); 35 | 36 | return $resolved 37 | ->when( 38 | fn (Collection $items) => $items->isEmpty(), 39 | fn (Collection $items) => $items->push($this->fallback($uri)) 40 | ) 41 | ->when($this->needSuffix($resolved, $suffix), static fn ($items) => $items->push($suffix)) 42 | ->implode('.'); 43 | } 44 | 45 | protected function resolve(string $uri): Collection 46 | { 47 | return Str::of($uri) 48 | ->rtrim(' /') 49 | ->explode('/') 50 | ->filter(fn (string $value) => $this->has($value)) 51 | ->map(fn (string $value) => $this->map($value)); 52 | } 53 | 54 | protected function getMethodSuffix(array $methods, string $uri): string 55 | { 56 | return $this->action->get($methods, $uri); 57 | } 58 | 59 | protected function has(string $value): bool 60 | { 61 | return ! empty($value) && Str::doesntContain($value, '{'); 62 | } 63 | 64 | protected function map(string $value): string 65 | { 66 | return (string) Str::of($value)->lower()->kebab(); 67 | } 68 | 69 | protected function needSuffix(Collection $haystack, string $needle): bool 70 | { 71 | return $haystack->count() <= 2 || $needle !== $haystack->last(); 72 | } 73 | 74 | protected function fallback(string $uri): string 75 | { 76 | if (Str::startsWith($uri, '{') && Str::endsWith($uri, '}') && Str::doesntContain($uri, '/')) { 77 | return Str::of($uri)->after('{')->before('}')->value(); 78 | } 79 | 80 | return $this->default; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Providers/RoutingServiceProvider.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames\Providers; 19 | 20 | use DragonCode\LaravelRouteNames\Routing\Router; 21 | use Illuminate\Foundation\Application; 22 | use Illuminate\Routing\RoutingServiceProvider as BaseRoutingServiceProvider; 23 | 24 | class RoutingServiceProvider extends BaseRoutingServiceProvider 25 | { 26 | protected function registerRouter(): void 27 | { 28 | $this->app->singleton('router', static fn (Application $app) => new Router($app['events'], $app)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Routing/Route.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames\Routing; 19 | 20 | use Closure; 21 | use DragonCode\LaravelRouteNames\Helpers\Name; 22 | use Illuminate\Routing\Route as BaseRoute; 23 | use Illuminate\Support\Str; 24 | 25 | use function app; 26 | use function config; 27 | 28 | class Route extends BaseRoute 29 | { 30 | public function __construct( 31 | array|string $methods, 32 | string $uri, 33 | array|Closure $action, 34 | protected Name $naming 35 | ) { 36 | parent::__construct($methods, $uri, $action); 37 | } 38 | 39 | public function getName(): ?string 40 | { 41 | if ($this->isProtectedRouteName()) { 42 | return $this->getProtectedRouteName(); 43 | } 44 | 45 | return app()->call($this->getRouteNamesExtender(), [ 46 | 'name' => $this->naming->get($this->methods(), $this->uri()), 47 | 'route' => $this, 48 | ]); 49 | } 50 | 51 | protected function isProtectedRouteName(): bool 52 | { 53 | if ($name = $this->getProtectedRouteName()) { 54 | return Str::is($this->getProtectedRouteNames(), $name); 55 | } 56 | 57 | return false; 58 | } 59 | 60 | protected function getProtectedRouteName(): ?string 61 | { 62 | return parent::getName(); 63 | } 64 | 65 | protected function getProtectedRouteNames(): array 66 | { 67 | return config('route.names.exclude'); 68 | } 69 | 70 | /** 71 | * @return (callable(string, self): string)|string 72 | */ 73 | protected function getRouteNamesExtender(): callable|string|null 74 | { 75 | return config('route.names.extender') ?: static fn (string $name): string => $name; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Routing/Router.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames\Routing; 19 | 20 | use DragonCode\LaravelRouteNames\Helpers\Name; 21 | use Illuminate\Routing\Router as BaseRouter; 22 | 23 | use function app; 24 | 25 | class Router extends BaseRouter 26 | { 27 | public function newRoute( 28 | $methods, // @pest-ignore-type 29 | $uri, // @pest-ignore-type 30 | $action // @pest-ignore-type 31 | ): Route { 32 | return (new Route($methods, $uri, $action, app(Name::class))) 33 | ->setRouter($this) 34 | ->setContainer($this->container); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2025 Andrey Helldar 11 | * @license MIT 12 | * 13 | * @see https://github.com/TheDragonCode/laravel-route-names 14 | */ 15 | 16 | declare(strict_types=1); 17 | 18 | namespace DragonCode\LaravelRouteNames; 19 | 20 | use Illuminate\Support\ServiceProvider as BaseServiceProvider; 21 | 22 | class ServiceProvider extends BaseServiceProvider 23 | { 24 | public function boot(): void 25 | { 26 | $this->publishConfig(); 27 | } 28 | 29 | public function register(): void 30 | { 31 | $this->registerConfig(); 32 | } 33 | 34 | protected function publishConfig(): void 35 | { 36 | $this->publishes([ 37 | __DIR__ . '/../config/route.php' => config_path('route.php'), 38 | ], 'config'); 39 | } 40 | 41 | protected function registerConfig(): void 42 | { 43 | $this->mergeConfigFrom( 44 | __DIR__ . '/../config/route.php', 45 | 'route' 46 | ); 47 | } 48 | } 49 | --------------------------------------------------------------------------------