我不解释为什么即使不应该调用这种中间件。
这是中间件:
<?php
namespace App\Http\Middleware;
use ...;
class FacebookLogin
{
public function handle($request, Closure $next)
{
Session::flash('error', "My message error");
return $next($request);
}
}
这是我的Kernel.php文件:
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
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' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'fb.login' => \App\Http\Middleware\FacebookLogin::class,
'cors' => \App\Http\Middleware\Cors::class,
'auth.api' => \App\Http\Middleware\AuthApi::class,
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
];
}
这是我的RouteServiceProvider.php文件:
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
public function boot()
{
parent::boot();
}
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapFacebookRoutes();
}
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Route::prefix('ajax')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/ajax.php'));
Route::middleware(['web', 'auth'])
->namespace($this->namespace)
->group(base_path('routes/auth.php'));
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
protected function mapFacebookRoutes()
{
Route::group([
'middleware' => ['web', 'fb.login'],
'namespace' => $this->namespace,
'prefix' => 'fb',
], function () {
require base_path('routes/facebook.php');
});
Route::group([
'middleware' => ['web', 'fb.login'],
'namespace' => $this->namespace,
'prefix' => 'fb/ajax',
], function () {
require base_path('routes/facebook_ajax.php');
});
}
}
我得到的是导航到网站的基本URL(例如:www.example.com)时显示的Flash消息。
但是,正如您在mapWebRoutes
函数中看到的那样,我仅将Web中间件应用于Web路由,所以我不为什么要调用FacebookLogin
中间件。
我仅将FacebookLogin
中间件应用于“ fb”和“ fb / ajax”前缀路由。
我注意到了另一个奇怪的行为:仅当我访问网站的https版本时,我才收到消息。
答案 0 :(得分:0)
在中间件中检查$request->has('error')
<?php
namespace App\Http\Middleware;
use ...;
class FacebookLogin
{
public function handle($request, Closure $next)
{
if($request->has('error')){
Session::flash('error', "My message error");
}
return $next($request);
}
}
在RouteServiceProvider更改单内:
public function map()
{
$this->mapApiRoutes();
$this->mapFacebookRoutes();
$this->mapWebRoutes();
}
您已在'fb.login' => \App\Http\Middleware\FacebookLogin::class,
内添加了protected $routeMiddleware
,因此它适用于所有路由,请从此处删除。