在同一个文件中kernel.php
你会发现中间件web
请求这是默认模式。
'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,
],
So web
用于有状态请求,带有会话、cookie、csrf 令牌、会话身份验证......
And api
用于无状态请求,因此没有上述功能,但使用另一个中间件throttle它通过 IP 限制每分钟的请求数量(检查限制配置,默认 60/mn)。
The 替代绑定两者都是通用的,它处理路由声明中配置的参数的绑定。
通常,您不会将这两个堆叠在一起。要使用它们,请使用已存在的文件web.php
and api.php
。这是如何运作的 ?检查内容App\Providers\RouteServiceProvider
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) //the value here is \App\Http\Controllers
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
如果您的项目可以分为更多组,您可以编辑此文件。
例如,我曾经用它来创建 5 个不同的组,每个组都有自己的路由文件,因为在公共实体(网络)之上我有 4 个不同的可验证实体(管理员、所有者、用户、审核……),每个组都有它自己的命名空间(控制器基本命名空间)和它自己的会话中间件