2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel5.3]テスト用に仕込むMiddleware(備忘録)

Posted at

前提

Laravel5.3のソースです。
備忘録代わりに投稿。

AppBeforeMiddlewareAppAfterMiddlewareを新規作成して指定を追加

app\Http\Middleware\AppBeforeMiddleware.php
<?php
namespace App\Http\Middleware;

use Closure;

class AppBeforeMiddleware
{
    public function handle($request, Closure $next, $guard = null)
    {
        // APP 開始時に実施する処理を記述
        logger()->info('- START -',[
            \IpCheckHelper::get(),
            $request->url(),
            $request->method(),
            $request->server("HTTP_USER_AGENT"),
            \Session::getId(),
            'memory start:'. memory_get_usage() / (1024 * 1024).'MB'
        ]);
        return $next($request);
    }
}
app\Http\Middleware\AppAfterMiddleware.php
namespace App\Http\Middleware;

use Closure;

class AppAfterMiddleware
{
    public function handle($request, Closure $next, $guard = null)
    {
        $response = $next($request);
        // APP 終了時に実施する処理を記述
        logger()->info('- END   -',[
            \IpCheckHelper::get(),
            $request->url(),
            $request->method(),
            $request->server("HTTP_USER_AGENT"),
            \Session::getId(),
            'memory end:'. memory_get_peak_usage() / (1024 * 1024).'MB',
            'memory peak:'. memory_get_peak_usage() / (1024 * 1024).'MB'
        ]);
        return $response;
    }
}
app\Http\Kernel.php
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        //APP開始時の処理
        \App\Http\Middleware\AppBeforeMiddleware::class,

        //他のmiddlewareを記述

        //APP終了時の処理
        \App\Http\Middleware\AppAfterMiddleware::class,
    ];

仕込んだ状態である程度テストやってメモリ使用量のMAXを見極めるとか。
\IpCheckHelper::get()はELB経由とかの時のIP取得元振り分けとかをやっている共通部品です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?