laravel - 具有可变前缀和条件的 Laravel 路由组
问题描述
我想在 Laravel 中创建一个以变量为前缀的路由组。我也需要设置一些条件。如何正确地做到这一点?
我在关注文档:https ://laravel.com/docs/8.x/routing#route-group-prefixes但只有一般示例。
此代码应创建 2 条路线:/{hl}/test-1
并且/{hl}/test-2
where {hl}
is limited to (en|pl)
,但它给出了一个错误:"Call to a member function where() on null"
Route::prefix('/{hl}')->group(function ($hl) {
Route::get('/test-1', function () {
return 'OK-1';
});
Route::get('/test-2', function () {
return 'OK-2';
});
})->where('hl','(en|pl)');
解决方案
该group
调用不返回任何内容,因此没有任何链接。如果您where
在调用 to 之前进行调用group
,类似于您调用 的方式prefix
,它将建立这些属性,然后当您调用group
它时,它将级联到组中的路由:
Route::prefix('{hl}')->where(['h1' => '(en|pl)'])->group(function () {
Route::get('test-1', function () {
return 'OK-1';
});
Route::get('test-2', function () {
return 'OK-2';
});
});
推荐阅读
- python - 如何让 python xmlsec 库在 Pivotal Cloud Foundry 上运行
- awk - 两条线之间的距离
- php - 警告:在第 1 行的 * 中使用未定义的常量 php - 假定为“php”(这将在 PHP 的未来版本中引发错误)
- c++ - C++ Poco - 如何向特定线程发送通知/消息?
- vue.js - vue cli 全局主体样式不包含在构建中
- android-studio - 此计算机不支持 Intel 虚拟化技术 (VT-x) 或被 Hyper-V 错误独占使用
- jsf - 如何关闭 websocket 服务器端点和会话范围的支持 bean 之间的差距
- .net - Nuget - 是否需要重新安装标有 requireReinstallation 问题的软件包?
- android-studio - Android Studio 将无限更新且不会停止
- matrix - 对于大型矩阵,Lua 中的一维或二维数组的计算效率更高吗?