laravel 命名资源无法正常工作并从头开始删除点

Posted

技术标签:

【中文标题】laravel 命名资源无法正常工作并从头开始删除点【英文标题】:laravel named resources not working properly and removes the dots from the beginning 【发布时间】:2021-08-18 01:33:54 【问题描述】:

问题

我正在尝试为我的路由组添加名称前缀:

Route::middleware(['auth:sanctum', 'verified'])->prefix('dashboard')->name('dashboard')->group(function () 
    
    Route::get('', [DashboardController::class, 'dashboard']);

    // other routes
    Route::resource('estates', EstateController::class, ['names' => '.estates' ])->except('show');
    // other routes
);

它删除了.。 (应该是 dashboard.estates.index 等...)

| POST      | dashboard/estates                       | dashboardestates.store          |
| GET|HEAD  | dashboard/estates                       | dashboardestates.index          |
| GET|HEAD  | dashboard/estates/create                | dashboardestates.create         |
| DELETE    | dashboard/estates/estate              | dashboardestates.destroy        |
| PUT|PATCH | dashboard/estates/estate              | dashboardestates.update         |
| GET|HEAD  | dashboard/estates/estate/edit         | dashboardestates.edit           |

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

测试 1

如果我将数组选项传递给names,它与点. 一起工作得很好。

Route::resource('estates', EstateController::class, ['names' => ['index' => '.estates.index'] ])->except('show');
| POST      | dashboard/estates                       | dashboardestates.store          |
| GET|HEAD  | dashboard/estates                       | dashboard.estates.index         |

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++

测试 2

// web.php
Route::resource('estates', EstateController::class, ['names' => '.estates.' ])->except('show');

路线列表:

| POST      | dashboard/estates                       | dashboardestates..store         |
| GET|HEAD  | dashboard/estates                       | dashboardestates..index         |
| GET|HEAD  | dashboard/estates/create                | dashboardestates..create        |
| DELETE    | dashboard/estates/estate              | dashboardestates..destroy       |
| PUT|PATCH | dashboard/estates/estate              | dashboardestates..update        |
| GET|HEAD  | dashboard/estates/estate/edit         | dashboardestates..edit          |

预期:dashboard.estates..index

得到:dashboardestates.index

【问题讨论】:

【参考方案1】:

您可以将其全部简化为:

Route::group(['middleware' => ['auth:sanctum', 'verified']], function () 
    
    Route::get('/dashboard', [DashboardController::class, 'dashboard'])
        ->name('dashboard');
    
    Route::group(['prefix' => '/dashboard', 'as' => 'dashboard.'], function () 
        Route::resource('estates', EstateController::class)->except('show');
    );

);

上述命名路由的输出将是dashboard.estates.method

这可以简化(即不重复或嵌套某些内容)仪表板的命名路由将是 dashboard.index 而不是 dashboard

【讨论】:

该组中有一个名为dashboard 的根。它会将路由更改为dashboard.。解决办法是什么? @alien 不确定您的意思,我没有看到名为 dashboard 的路线。检查here for an example 编辑了帖子。看第一个代码示例。仪表板有一个主页。编写重复的代码也不干净。它是怎样的最好的方式。主要问题仍然存在:为什么 laravel 从名称的开头删除 . 以及如何停止它。 好的,我接受了你的回答。请接受编辑。我认为应该如何更好地解决这个问题。

以上是关于laravel 命名资源无法正常工作并从头开始删除点的主要内容,如果未能解决你的问题,请参考以下文章

来自git clone的Laravel项目无法正常工作(无法使用key:generate或composer install / update)

完全重置 Laravel 5 迁移的东西?

全新安装后 Laravel 页面无法正常工作

Github CICD 测试在 laravel 中无法正常工作?

IBM MobileFirst 7.1 - 本地服务器复制资源

Laravel 队列作业不加载文件