- 1. 概要
- 2. Job
- 3. Middleware
- 4. 組み込み
- 5. 参考サイト
1. 概要
カウンタ処理を実装しております。
もう、そんなの必要ないという話もありますが、「apache」のみでは不足する、アクセスの解析のためにも使っています。
この処理がだんだん遅くなりまして。
「AppServiceProvider::boot()」に置いておったですが、コンテンツ表示までに時間がかかるし、負担になってきました。
「ChatGPT」軍曹に相談すると、「Controller」「Middleware」等で「view()」実行後に処理する方法があるとのこと。
やってみます。
2. Job
まず、実行する「Job」のクラスを作成します。
「Laravel」のドキュメントルートで。
(次項も含め、パーミッションが面倒なので、ディレクトリやファイルの作成は、いったん「root」ユーザ権限で作成した後、所有者を変えるのがいいかと思います)
mkdir -pv app/Jobs
php artisan make:job CounterJob
こんなん出来上がります。
app/Jobs/CounterJob.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CounterJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
//
}
}
「class」宣言の、「use」の後に、メンバ変数を定義しておいて。
protected string $ip;
protected string $path;
protected ?string $referer;
protected ?string $userAgent;
「__construct()」メソッドで受け取った引数をメンバ変数に設定して。
public function __construct(string $ip, string $path, ?string $referer, ?string $userAgent)
{
$this->ip = $ip;
$this->path = $path;
$this->referer = $referer;
$this->userAgent = $userAgent;
}
「handle()」メソッドの中に処理を記述します。
3. Middleware
php artisan make:middleware CounterMiddleware
こんなん出来上がります。
app/Http/Middleware/CounterMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class CounterMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return $next($request);
}
}
「use」に下記の行を追加して。
use App\Jobs\CounterJob;
「handle()」メソッドの中に処理を記述します。
public function handle($request, Closure $next)
{
$response = $next($request);
CounterJob::dispatchAfterResponse(
$request->ip(),
$request->path(),
$request->headers->get('referer'),
$request->userAgent()
);
return $response;
}
「$next($request)」で、「view()」の結果を受け取っておいて、「CounterJob::dispatchAfterResponse()」で「job」を登録して、「view()」の結果を返すらしい。
4. 組み込み
「MiddleWare」を「app/Http/Kernel.php」に組み込みます。
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
「web」の配列の中に、次の行を組み込みます。
(これ、「Laravel Framework 10.48.29」の書き方なので、ご注意)
\App\Http\Middleware\CounterMiddleware::class,
ここまでやったら、「app/Jobs/CounterJob.php」の「handle()」の中に、適当なログでも入れて。
「Laravel」のキャッシュをクリア後、アクセスして、ログが出力されるか、確認します。
5. 参考サイト
本ページは、「ChatGPT」軍曹を参考にさせていただきました。
|