【发布时间】:2021-01-07 22:33:59
【问题描述】:
在我的第二次尝试中,我通过使用Route::group 和prefixes 在使用routeMiddleware Kernel 的URL 的第一段中的{locale} 为我正在开发的网站实现了多语言实现。它工作得很好,除非用parameters 检索路由Resources。
该实现有一个小问题,因为某种原因它将parameter 变成%2F{id}(这是不正确的)并且不检索我的PublicGalleriesController 请求的资源。我不明白为什么,因为当我将鼠标悬停在生成的锚点 href 上时,我看到了正确的 url 格式。但是当我点击它时,会给出一个带有混乱 url 的 404 Not Found 消息。
web.php这是我的路由组,用一个函数封装了所有路由
Route::group([
'prefix' => '{locale}',
'middleware' => 'setlocale',
], function() {
// all my routes are within this route group including:
Route::resource('gallery', 'PublicGalleriesController');
Auth::routes();
Route::group(['middleware' => 'auth'], function() {
...
});
});
App/Http/Middleware/Localisation.php 路由通过Kernel.php路由的中间件
public function handle($request, Closure $next)
{
\App::setLocale($request->segment(1));
return $next($request);
}
PublicGalleriesController.php 从模型中检索图像路径并将其返回到客户端视图
public function show($id)
{
// Show gallery group images for given group id
$pics = null;
$path = null;
$path = GalleryGroup::find($id);
$pics = Gallery::select('imagefilename', 'group_id')->where('group_id', $id)->orderBy('id', 'asc')->get()->toArray();
return view('gallery.show', compact('pics', 'path'));
}
当我将鼠标悬停在index.blade 上可见的画廊组照片链接上时,它在浏览器左角显示为:localhost/en/gallery/41。 index.blade 检索图库组主键并在循环中构建 html 锚链接:<a href="{{ url(app()->getLocale(), 'gallery/' . $item['id']) }}">{{$item['descrp']}}</a>
当我单击此链接时,它应该通过PublicGalleriesController 运行show 函数并检索所有这些画廊组照片,但返回一个404 Not Found,浏览器中的URL 显示localhost/en/gallery%2F41。 %2F 我相信是一个 Url 编码的正斜杠。
php artisan route:list 显示show 资源如下:
| Domain | Method | URI | Name | Action
| Middleware |
+--------+-----------------------------------------+--------------+-----------------------
| | GET|HEAD | {locale}/gallery/{gallery} | gallery.show | App\Http\Controllers\PublicGalleriesController@show
| web,setlocale |
有人可以帮我理解为什么网址变得如此混乱吗?
Laravel 版本:5.6.39
【问题讨论】:
-
附注,您应该在发布问题时始终包含您使用的 Laravel 版本
标签: php laravel routes laravel-routing laravel-resource