「Deprecated: Use of `BaseErrorHandler` and subclasses are deprecated. 」の対処法

「Deprecated: Use of `BaseErrorHandler` and subclasses are deprecated. 」の対処法

104 回閲覧されました

みなさんこんにちは、jonioです。

新しく働く会社ではCakePHPを使うので勉強しようと思ってネットの情報を勉強をしてCakePHPの4.1をインストールしてトップページを表示したら画面上に「Deprecated: Use of `BaseErrorHandler` and subclasses are deprecated. Upgrade to the new `ErrorTrap` and `ExceptionTrap` subsystem. See https://book.cakephp.org/4/en/appendices/4-4-migration-guide.html /Applications/MAMP/htdocs/cakephp/CakeBlog/config/bootstrap.php, line: 130 You can disable all deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED`. Adding `config/bootstrap.php` to `Error.ignoredDeprecationPaths` in your `config/app.php` config will mute deprecations from that file only. in /Applications/MAMP/htdocs/cakephp/CakeBlog/vendor/cakephp/cakephp/src/Core/functions.php on line 318」と表示されました。

私と同じようにCakePHPを勉強していきなりエラーが出てどうしようもなくなった人の為にこの記事を残します。

エラー文のURLを見る

エラー文の「Deprecated: Use of `BaseErrorHandler` and subclasses are deprecated. Upgrade to the new `ErrorTrap` and `ExceptionTrap」は「BaseErrorHandlerとsubclassesを使うのは廃止されました。ErrorTrapとExceptionTrapはアップグレードしましょう」という意味です。

この部分で記述でエラーが出ているのが分かります。

エラー文に「See https://book.cakephp.org/4/en/appendices/4-4-migration-guide.html 」とあるのでURLにアクセスして「BaseErrorHandler」の項目(下記参照)を見ます。

エラー文の続きを読むと「Adding `config/bootstrap.php`」とあるので「上記の赤枠をconfig/bootstrap.phpに記述しなさい」という意味だと思います。

bootstrap.phpの修正

「CakePHPのプロジェクト > config > bootstrap.php」のコードの追加・修正をします、下記に変更します。

<?php
declare(strict_types=1);

/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         0.10.8
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */

/*
 * Configure paths required to find CakePHP + general filepath constants
 */
require __DIR__ . '/paths.php';

/*
 * Bootstrap CakePHP.
 *
 * Does the various bits of setup that CakePHP needs to do.
 * This includes:
 *
 * - Registering the CakePHP autoloader.
 * - Setting the default application paths.
 */
require CORE_PATH . 'config' . DS . 'bootstrap.php';

use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Datasource\ConnectionManager;
use Cake\Error\ConsoleErrorHandler;
use Cake\Error\ErrorHandler;
use Cake\Http\ServerRequest;
use Cake\Log\Log;
use Cake\Mailer\Mailer;
use Cake\Mailer\TransportFactory;
use Cake\Routing\Router;
use Cake\Utility\Security;


//ここから追加
use Cake\Error\ErrorTrap;
use Cake\Error\ExceptionTrap;
//ここまで追加


/*
 * See https://github.com/josegonzalez/php-dotenv for API details.
 *
 * Uncomment block of code below if you want to use `.env` file during development.
 * You should copy `config/.env.example` to `config/.env` and set/modify the
 * variables as required.
 *
 * The purpose of the .env file is to emulate the presence of the environment
 * variables like they would be present in production.
 *
 * If you use .env files, be careful to not commit them to source control to avoid
 * security risks. See https://github.com/josegonzalez/php-dotenv#general-security-information
 * for more information for recommended practices.
*/
// if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
//     $dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
//     $dotenv->parse()
//         ->putenv()
//         ->toEnv()
//         ->toServer();
// }

/*
 * Read configuration file and inject configuration into various
 * CakePHP classes.
 *
 * By default there is only one configuration file. It is often a good
 * idea to create multiple configuration files, and separate the configuration
 * that changes from configuration that does not. This makes deployment simpler.
 */
try {
    Configure::config('default', new PhpConfig());
    Configure::load('app', 'default', false);
} catch (\Exception $e) {
    exit($e->getMessage() . "\n");
}

/*
 * Load an environment local configuration file to provide overrides to your configuration.
 * Notice: For security reasons app_local.php **should not** be included in your git repo.
 */
if (file_exists(CONFIG . 'app_local.php')) {
    Configure::load('app_local', 'default');
}

/*
 * When debug = true the metadata cache should only last
 * for a short time.
 */
if (Configure::read('debug')) {
    Configure::write('Cache._cake_model_.duration', '+2 minutes');
    Configure::write('Cache._cake_core_.duration', '+2 minutes');
    // disable router cache during development
    Configure::write('Cache._cake_routes_.duration', '+2 seconds');
}

/*
 * Set the default server timezone. Using UTC makes time calculations / conversions easier.
 * Check http://php.net/manual/en/timezones.php for list of valid timezone strings.
 */
date_default_timezone_set(Configure::read('App.defaultTimezone'));

/*
 * Configure the mbstring extension to use the correct encoding.
 */
mb_internal_encoding(Configure::read('App.encoding'));

/*
 * Set the default locale. This controls how dates, number and currency is
 * formatted and sets the default language to use for translations.
 */
ini_set('intl.default_locale', Configure::read('App.defaultLocale'));

/*
 * Register application error and exception handlers.
 */
$isCli = PHP_SAPI === 'cli';
if ($isCli) {
    // (new ConsoleErrorHandler(Configure::read('Error')))->register();     //この行を消す
    (new ErrorTrap(Configure::read('Error')))->register();                  //この行を追加
} else {
    // (new ErrorHandler(Configure::read('Error')))->register();            //この行を消す
    (new ExceptionTrap(Configure::read('Error')))->register();              //この行を追加
}


//以下の行は省略

これでエラー文がなくなります。