laravel 中间件的创建及使用
Laiyong Wang Lv5

PS:简单介绍下

创建中间件
1
php artisan make:middleware WangLaiYong
文件及内容

app/Http/Middleware/WangLaiYong.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class WangLaiYong
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$request->desc = '我是大帅逼';
return $next($request);
}
}

使用:在 api 请求时使用

app/Http/Kernel.php

1
2
3
4
5
6
7
8
9
10
11
12
protected $middlewareGroups = [
'web' => [
.
.
.
],
'api' => [
\App\Http\Middleware\WangLaiYong::class,//添加这行
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
 Comments