创建中间件
php artisan make:middleware PrintHelloworld
自动生成了 middleware
文件:app\Http\Middleware\PrintHelloworld.php
修改中间件文件
app\Http\Middleware\PrintHelloworld.php
<?php
namespace App\Http\Middleware;
use Closure;
class PrintHelloworld
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
dump('Hello World!');
return $next($request);
}
}
注册中间件
修改 app\Http\kernel.php
文件;有全局注册、grand注册、路由注册三种方式;
全局注册:作用于所有路由
.
.
.
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\PrintHelloworld::class,
];
.
.
.
访问主页, dump
语句会输首先输出。
路由注册:作用于注册的路由
修改 app\Http\kernel.php
中的 protected $routeMiddleware
部分;
.
.
.
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'printinfo' => \App\Http\Middleware\PrintHelloworld::class,
];
.
.
.
指定单个路由
修改 routes\web.php
文件
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
})->middleware('printinfo');
这样,这个中间件只会在访问指定路由时起作用;
指定一组路由
修改 routes\web.php
文件
<?php
use Illuminate\Support\Facades\Route;
Route::resource('test', 'TestController')->middleware('printinfo');
也可以在控制器中的 construct
函数中调用
public function __construct()
{
//这种方式会对该控制器里的所有方法都产生作用
//$this->middleware('printinfo);
//这种方式指定对控制器里的某个方法产生作用
$this->middleware('checklog', [
'only' => ['edit', 'update']
]);
//这种方式排除控制器里的某个方法
$this->middleware('checklog', [
'except' => ['edit', 'update']
]);
}
作用于 grand
修改 app\Http\kernel.php
中的 protected $middlewareGroups
部分;
.
.
.
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\PrintHelloworld::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
.
.
.
网友评论