php ライブラリ「filp/whoops」を使ってエラー画面を見やすくに表示する

php ライブラリ「filp/whoops」を使ってエラー画面を見やすくに表示する

phpでライブラリ「filp/whoops」を使ってエラー画面を見やすくキレイに表示するまでのサンプルコードを記述してます。composerを使用して「filp/whoops」はインストールしてます。

環境

  • OS windows10 pro 64bit
  • Apache 2.4.43
  • PHP 7.4.5
  • Composer 1.10.5

※windows10にApacheのインストールはこちら
※windows10にphpのインストールはこちら
※Windows10にComposerのインストールはこちら

filp/whoopsインストール

phpが動作しているフォルダで、composerを使用してインストールします。

composer require filp/whoops

<出力結果>
Using version ^2.7 for filp/whoops
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing filp/whoops (2.7.3): Downloading (100%)
filp/whoops suggests installing whoops/soap (Formats errors as SOAP responses)
Package mschop/noteephp is abandoned, you should avoid using it. Use mschop/notee instead.
Writing lock file
Generating autoload files
20 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

自分の場合は、下記にインストールしました。

filp/whoops利用

「C:\Apache24\htdocs」に「test.php」を下記のコードで作成します。
※ここでは存在しないファイル「nofind.txt」を指定してエラーを発生させてます。

<?php

require_once __DIR__ . '/vendor/autoload.php';

$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();

$text = file_get_contents("nofind.txt");

実行結果を確認すると、エラー画面がわかりやすくキレイに表示されていることが確認できます。