Laravel 本地化干扰路由资源参数

Laravel Localisation messing with Route Resource parameter

在我的第二次尝试中,我通过使用 Route::group 为我正在使用的网站实现了多语言实现,其中 prefixes {locale} 在 url 使用 routeMiddleware Kernel。除了使用 parameters.

检索路线 Resources 外,它工作得很好

实施有一个小问题,由于某种原因它将 parameter 变成了 %2F{id}(这是不正确的)并且没有检索我的 PublicGalleriesController 请求的资源。我不明白为什么,因为当我将鼠标悬停在生成的锚 href 上时,我看到了正确的 url 格式。但是当我点击它时,它给出了一条 404 Not Found 消息,其中包含乱七八糟的 url.

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 从模型中检索图像路径并 returns 它到客户端视图

    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 上可见的画廊合影 link 上时,它在浏览器左上角显示为:localhost/en/gallery/41index.blade 检索图库组主键并在循环中构建 html 锚点 link:<a href="{{ url(app()->getLocale(), 'gallery/' . $item['id']) }}">{{$item['descrp']}}</a>

当我单击此 link 时,它应该通过 PublicGalleriesController 运行 show 函数检索所有这些画廊组照片,而不是 returns 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                           |

有人可以帮我理解为什么 url 变得这么乱吗?

Laravel版本:5.6.39

url 助手的签名是:

function url($path = null, $parameters = [], $secure = null)

$parameters 是一个参数数组,而不是路径,因此它正在对您正在使用的 / 进行编码,因为参数是 URL 的一部分而不是路径.

您可以调整对 url 的调用以获得更完整的路径或使用 parameters 数组作为附加段,而不是路径:

url(app()->getLocale() .'/gallery/'. $item['id']); // 1 argument
url(app()->getLocale() .'/gallery', [$item['id']]); // 2 arguments
url(app()->getLocale(), ['gallery', $item['id']]);  // 2 arguments

您还可以使用 route 助手:

route('gallery.show', ['locale' => app()->getLocale(), 'gallery' => $item['id']]);

如果你不想为你想用 route 助手生成的所有 URL 传递 locale,你可以调整你的中间件来设置一个此参数的默认值:

public function handle($request, Closure $next)
{
    App::setLocale($locale = $request->route('locale'));

    URL::defaults(['locale' => $locale]); // set default

    return $next($request);
}

现在您不必传递该参数:

route('gallery.show', ['gallery' => ...]);

如果您不想将 locale 参数传递给您的所有路由“操作”(控制器方法和闭包),也可以这样做。您可以在 return 响应之前向该中间件添加这样一行:

$request->route()->forgetParameter('locale');