刀片中的 Laravel 8 路线

Posted

技术标签:

【中文标题】刀片中的 Laravel 8 路线【英文标题】:Laravel 8 Route in blade 【发布时间】:2022-01-15 06:23:23 【问题描述】:

我的 Laravel 项目中有以下网络路由

Route::resource('client/client/users', UserController::class);
Route::resource('users', UserController::class);

对于后者,我可以在我的刀片文件中使用 route('users.create') ,但我不确定为另一个文件添加什么。

我需要 2 种方式分离资源,因为一些用户能够从他们自己的客户端 (/users) 和其他客户端的用户 (/client/123/users) 编辑用户

`php artisan route:list 表明我得到了相同的名称。命名 clients-users 资源的正确方法是什么?

|        | GET|HEAD  | client/client/users             | users.index                     | App\Http\Controllers\UserController@index                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | POST      | client/client/users             | users.store                     | App\Http\Controllers\UserController@store                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/client/users/create      | users.create                    | App\Http\Controllers\UserController@create                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | PUT|PATCH | client/client/users/user      | users.update                    | App\Http\Controllers\UserController@update                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | DELETE    | client/client/users/user      | users.destroy                   | App\Http\Controllers\UserController@destroy                                     | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/client/users/user      | users.show                      | App\Http\Controllers\UserController@show                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | client/client/users/user/edit | users.edit                      | App\Http\Controllers\UserController@edit                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |

|        | GET|HEAD  | users                             | users.index                     | App\Http\Controllers\UserController@index                                       | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/create                      | users.create                    | App\Http\Controllers\UserController@create                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | DELETE    | users/user                      | users.destroy                   | App\Http\Controllers\UserController@destroy                                     | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | PUT|PATCH | users/user                      | users.update                    | App\Http\Controllers\UserController@update                                      | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/user                      | users.show                      | App\Http\Controllers\UserController@show                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |
|        | GET|HEAD  | users/user/edit                 | users.edit                      | App\Http\Controllers\UserController@edit                                        | web                                                       |
|        |           |                                   |                                 |                                                                                 | App\Http\Middleware\Authenticate:sanctum                  |
|        |           |                                   |                                 |                                                                                 | Illuminate\Auth\Middleware\EnsureEmailIsVerified          |

【问题讨论】:

只要php artisan route:list,就可以看到所有名称/URI/...的路由了 @DengSihan 查看我的编辑 为此使用正常路线。 Route::get('client/client/users', [UserController::class, 'clients'])->name('users.clients'); route('users.clients', ['client' => $client_id]); 【参考方案1】:

resource 的第三个参数是一个选项数组,其中一个允许您指定前缀。

Route::resource('client/client/users', UserController::class, ['as' => 'clients']);
Route::resource('users', UserController::class);

现在你的路线应该是

clients.users.action
users.action

【讨论】:

【参考方案2】:

你需要给它一个名字。也就是说,我认为您不希望 Route::resource 用于该路线。

我会给它起名字clients.users.index,但你可以给它起任何你想要的名字。

Route::get('clients/client/users', [ClientController::class, 'users'])->name('clients.users.index');
 route('clients.users.index') 

处理嵌套资源的方法是声明路由:

Route::resource('clients.users', SomeOtherController::class);

【讨论】:

以上是关于刀片中的 Laravel 8 路线的主要内容,如果未能解决你的问题,请参考以下文章

根据当前翻译的 Laravel 路线在刀片中添加“活动”类

Laravel - 检查刀片上的路线

Laravel 8将数据从控制器发送到刀片[重复]

在刀片视图中显示路线的结果(Laravel)

laravel 刀片 - 如何隐藏特定路线的 html 元素的显示

laravel 制作自己的刀片文件路径