使用redirect()时,Laravel 5.4语言环境恢复为默认设置

使用redirect()时,Laravel 5.4语言环境恢复为默认设置,redirect,routes,localization,laravel-5.4,Redirect,Routes,Localization,Laravel 5.4,我正在和拉雷维尔的新项目现场斗争。我在谷歌上搜索了很多次,但他们没有解决我的问题 然后我遵循了source,但它只在我调用pages byview return view('home'); 当我使用路由时,它不起作用 return redirect()->route('home'); 这是我的档案: web.php Route::get('/lang/{locale}', function ($locale) { App::setLocale($locale);

我正在和拉雷维尔的新项目现场斗争。我在谷歌上搜索了很多次,但他们没有解决我的问题

然后我遵循了source,但它只在我调用pages byview

  return view('home');
当我使用路由时,它不起作用

  return redirect()->route('home');
这是我的档案:

web.php

Route::get('/lang/{locale}', function ($locale) {
    App::setLocale($locale);
    Session::put('locale', $locale);
    //return view('home');                         ###### This one works
    return redirect()->route('home');              ###### Where as this does NOT work
});

Route::get('/', function () {
    return view('welcome');
});

Route::get('/home', 'HomeController@index')->name('home');
<div class="panel-body">
    You are logged in!
    {{__('auth.success')}}
    <br>
    <a href="/lang/ru">Rus</a>
    <br>
    <a href="/lang/kg">Kgz</a>
</div>
home.blade.php

Route::get('/lang/{locale}', function ($locale) {
    App::setLocale($locale);
    Session::put('locale', $locale);
    //return view('home');                         ###### This one works
    return redirect()->route('home');              ###### Where as this does NOT work
});

Route::get('/', function () {
    return view('welcome');
});

Route::get('/home', 'HomeController@index')->name('home');
<div class="panel-body">
    You are logged in!
    {{__('auth.success')}}
    <br>
    <a href="/lang/ru">Rus</a>
    <br>
    <a href="/lang/kg">Kgz</a>
</div>
提前感谢;)

好吧,在我换衣服之后

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/home', 'HomeController@index')->name('home')->middleware('changeLocale');
它开始起作用了

那么,为什么我的中间件不能工作呢


我是否应该将中间件分别分配给我的所有路由?

您可以将
changeLocale
中间件添加到
app/Http/Kernel.php中的
web
组:

protected $middlewareGroups = [
    'web' => [
        ...
        \Your\ChangeLocateMiddleware::class
    ],
    ...
]

正如您在
app/Providers/RouteServiceProvider.php
中所看到的,此中间件组适用于所有
web
路由。

创建一个路由组,将中间件应用于除
lang/{locale}
以外的路由。