W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
有時(shí)可能需要在 HTTP
響應(yīng)之后做一些工作。 如果你在中間件上定義了一個(gè) terminate
方法,并且你使用的是 FastCGI,那么 terminate
方法會(huì)在響應(yīng)發(fā)送到瀏覽器之后自動(dòng)調(diào)用:
<?php
namespace Illuminate\Session\Middleware;
use Closure;
class StartSession
{
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
// 存儲(chǔ) session 數(shù)據(jù)
}
}
terminate
方法應(yīng)該同時(shí)接收請(qǐng)求和響應(yīng)。定義了這個(gè)中間件之后,別忘了將它添加到路由列表或者 app/Http/Kernel.php
文件的全局中間件中。
當(dāng)你在中間件上調(diào)用 terminate
方法的時(shí)候,Laravel 將從 服務(wù)容器 中解析出一個(gè)新的中間件實(shí)例。如果在調(diào)用 handle
和 terminate
方法的同時(shí)使用相同的中間件實(shí)例, 請(qǐng)使用容器的 singleton
方法注冊(cè)中間件, 通常這應(yīng)該在 AppServiceProvider.php
文件中的 register
方法中完成:
use App\Http\Middleware\TerminableMiddleware;
/**
* 注冊(cè)任意應(yīng)用服務(wù)
*
* @return void
*/
public function register()
{
$this->app->singleton(TerminableMiddleware::class);
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: