2017-01-02 166 views
1

最近我已經升級到Laravel 5.3Laravel 5.2之後,我發現Localization中間件,我創建用於設置用戶的語言已停止工作,不知道爲什麼?Laravel 5.3中間件後衛不工作

我使用的是基於authenticationtoken

中間件是:

<?php namespace App\Http\Middleware; 

use Closure; 
use PhpSpec\Exception\Exception; 
use Illuminate\Support\Facades\Auth; 


class Localization { 

    public function handle($request, Closure $next, $guard = null) 
    { 
     if(!Auth::guard()->guest()) 
     { 
      if(!session('current_locale')) { 
       $localecode = Auth::guard($guard)->user()->getLocale(); 
       session('current_locale',empty ($localecode) ? 'en' : $localecode); 
       \App::setLocale($localecode); 
      }else{ 
       \App::setLocale(session('current_locale')); 
      } 
     } 
     return $next($request); 
    } 

} 

而且我routes/api.php是:

Route::group(['prefix' => 'api/service/v1', 'middleware' => ['api'], 'guard' => 'api'], function(){ 
    Route::get("someUrlHere","[email protected]"); 
    Route::group(['middleware' => ['auth:api','localization','beforeMiddleware', 'afterApiCallMiddleware'], 'guard' => 'api'], function() { 
     Route::get("SomeOtherUrlHere","[email protected]"); 
    }); 
}); 

config/auth.php是:

<?php 

return [ 
    'defaults' => [ 
     'guard' => 'web', 
     'passwords' => 'users', 
    ], 

    'guards' => [ 
     'web' => [ 
      'driver' => 'session', 
      'provider' => 'users', 
     ], 

     'api' => [ 
      'driver' => 'token', 
      'provider' => 'users', 
     ], 
    ], 

    'providers' => [ 
     'users' => [ 
      'driver' => 'eloquent', 
      'model' => App\Models\User::class, 
     ], 
    ], 

    'passwords' => [ 
     'users' => [ 
      'provider' => 'users', 
      'email' => 'auth.emails.password', 
      'table' => 'password_resets', 
      'expire' => 60, 
     ], 
    ], 
]; 

app/Http/Kernal.php是:

<?php namespace App\Http; 

use Illuminate\Foundation\Http\Kernel as HttpKernel; 

class Kernel extends HttpKernel { 

    protected $middleware = [ 
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
     'Barryvdh\Cors\HandleCors', 

    ]; 

    protected $middlewareGroups = [ 
     'web' => [ 
      \App\Http\Middleware\EncryptCookies::class, 
      \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
      \Illuminate\Session\Middleware\StartSession::class, 
      \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
      \App\Http\Middleware\VerifyCsrfToken::class, 
      \Illuminate\Routing\Middleware\SubstituteBindings::class, 
     ], 

     'api' => [ 
      'throttle:60,1', 
      'bindings', 
     ], 
    ]; 

    protected $routeMiddleware = [ 
     'auth' => 'App\Http\Middleware\Authenticate', 
     'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',  
     'guest' => 'App\Http\Middleware\RedirectIfAuthenticated', 
     'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
     'localization'=>'App\Http\Middleware\Localization', 
     'afterMiddleware' => 'App\Http\Middleware\AfterMiddleware', 
     'beforeMiddleware' => 'App\Http\Middleware\BeforeMiddleware', 
     'afterApiCallMiddleware' => 'App\Http\Middleware\AfterApiCall', 
     'can' => \Illuminate\Auth\Middleware\Authorize::class, 
     'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    ]; 

} 

每次調用該方法時,我發現$guardhandle()方法的Localization中間件的方法。

+0

顯示您的auth.php – msonowal

+0

好的。讓我發佈它。 – Dev

+0

@msonowal我已經發布了它。 – Dev

回答

2

由於$guard是中間件中的附加參數,因此您必須手動將其傳遞。你應該能夠從這樣做可以改變你的路線::組中間件陣列值:

'localization' 

到:

'localization:api' 

希望這有助於!

+0

它的工作原理。非常感謝。 – Dev