1
最近我已經升級到Laravel 5.3
從Laravel 5.2
之後,我發現Localization
中間件,我創建用於設置用戶的語言已停止工作,不知道爲什麼?Laravel 5.3中間件後衛不工作
我使用的是基於authentication
的token
。
中間件是:
<?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,
];
}
每次調用該方法時,我發現$guard
爲handle()
方法的Localization
中間件的方法。
顯示您的auth.php – msonowal
好的。讓我發佈它。 – Dev
@msonowal我已經發布了它。 – Dev