dragon-code/laravel-route-names

自动生成路由名称

1.5.0 2024-03-13 07:58 UTC

This package is auto-updated.

Last update: 2024-09-03 10:59:57 UTC


README

the dragon code route names

Stable Version Unstable Version Total Downloads Github Workflow Status License

安装

要获取最新版本的 Laravel Route Names,只需使用 Composer 引入项目。

composer require dragon-code/laravel-route-names

接下来,在 bootstrap/app.php 文件中将 Illuminate\Foundation\Application 替换为 DragonCode\LaravelRouteNames\Application

现在您可以列出路由,例如通过调用 php artisan route:list 命令或使用 dragon-code/pretty-routes 包。

使用方法

这就完了。现在您无需指定路由名称。现在路由名称将根据项目的最终URL自动生成。

将忽略应用程序中所有之前指定的路由名称。

与所有扩展路由列表的包解决方案兼容。

此外,即将支持 Laravel Idea

由于路由名称是在接收到它们的时候生成的,我们建议在生产环境中使用路由缓存。

php artisan route:cache

// or

php artisan optimize

基本路由

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']);

资源路由

app('router')->resource('authors/{author}/photos', Author\PhotoController::class);

API 资源路由

app('router')->apiResource('authors/{author}/photos', Author\PhotoController::class);

排除列表

通过发布配置文件,您可以使用 artisan 命令显式指定不需要翻译的路由名称掩码。

php artisan vendor:publish --provider="DragonCode\LaravelRouteNames\ServiceProvider"

异常

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.

结果

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']);
    });

之前

GET     /pages         pages.
POST    /pages         pages.
GET     /pages/{page}  pages.
DELETE  /pages/{page}  pages.

之后

GET     /pages         pages.index
POST    /pages         pages.store
GET     /pages/{page}  pages.show
DELETE  /pages/{page}  pages.destroy

许可证

本软件包采用 MIT 许可证