Laravel locale中间件不适用于所有路由&注销后,语言环境会话会破坏

时间:2016-11-27 03:43:43

标签: laravel-5 locale laravel-middleware

我正在使用多语言网站。我已经为locale创建了一个中间件。 Locale与Home,Login&注册视图。但是,它无法与auth/passwords/reset.blade.phpauth/passwords/email.blade.php一起使用。当我按下注销时,Locale会话会破坏。

  

Laravel 5.3 routes / web.php

Route::group(['middleware' => 'locale'], function(){
    Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

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

});

Route::get('setlocale/{locale}', function($locale){
     \Session::put('locale', $locale);
     return back();
}); 
  

中间件:语言环境

<?php

namespace Onebook\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Config;

class Locale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        $locale = 'en';
        if(Session::has('locale')){
            $locale = Session::get('locale');
        }
        else{
            $locale = Config::get('app.locale');
        }
        \App::setlocale($locale);
        return $next($request);
    }
}

0 个答案:

没有答案