FuelPHPでThemeごとにOops!の画面を変えたときのメモ。
FuelPHPはFUEL_ENVがproductionの時に下記のviewを表示しています。
fuel/core/views/erros/production.php
production.phpがどこで設定されているかというと
fuel/core/classes/errorhandler.php
内のここです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Shows the errors/production view and exits. This only gets * called when an error occurs in production mode. * * @return void */ protected static function show_production_error($e) { // when we're on CLI, always show the php error if (\Fuel::$is_cli) { return static::show_php_error($e); } if ( ! headers_sent()) { $protocol = \Input::server('SERVER_PROTOCOL') ? \Input::server('SERVER_PROTOCOL') : 'HTTP/1.1'; header($protocol.' 500 Internal Server Error'); } exit(\View::forge('errors'.DS.'production')); } |
Themeごとにエラーを表示するには\View::forge()ではなく、\Theme::instance()->view()を使う必用があります。
そこで、errorhandler.php を拡張します。
fuel/core/classes/errorhandler.php を fuel/app/classes/core/errorhandler.php
にコピーします。
下記のように書き換えます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php /** * */ class Errorhandler extends \Fuel\Core\Errorhandler { /** * Shows the errors/production view and exits. This only gets * called when an error occurs in production mode. * * @return void */ protected static function show_production_error($e) { // when we're on CLI, always show the php error if (\Fuel::$is_cli) { return static::show_php_error($e); } if ( ! headers_sent()) { $protocol = \Input::server('SERVER_PROTOCOL') ? \Input::server('SERVER_PROTOCOL') : 'HTTP/1.1'; header($protocol.' 500 Internal Server Error'); } exit(\Theme::instance()->view('errors'.DS.'production')); } } |
fuel/app/bootstrap.php を修正します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php // Bootstrap the framework DO NOT edit this require COREPATH.'bootstrap.php'; \Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Errorhandler' => APPPATH . 'classes/core/errorhandler.php', // 追記 )); // Register the autoloader \Autoloader::register(); /** * Your environment. Can be set to any of the following: * * Fuel::DEVELOPMENT * Fuel::TEST * Fuel::STAGING * Fuel::PRODUCTION */ \Fuel::$env = \Arr::get($_SERVER, 'FUEL_ENV', \Arr::get($_ENV, 'FUEL_ENV', \Fuel::DEVELOPMENT)); // Initialize the framework with the config file. \Fuel::init('config.php'); |
これで拡張は完了です。
続いて、fuel/app/themes/default/errors/にproduction.php を作成します。
1 2 3 |
<div> サーバー内部エラーが発生しています。 </div> |
とエラーページを書き換えることができます。
よければお試しください。