3

http://kohanaframework.org/3.0/guide/kohana/tutorials/error-pagesしかし、何らかの理由でHTTP_Exception_404をキャッチできません。それでも、カスタムページではなく、醜いエラーページが表示されます。 。

また、URL error / 404 / Messageを入力すると、醜いコハナHTTP404エラーメッセージが表示されます。

ファイルの構造は次のとおりです。

  • モジュール
    • 私の
      • init.php
      • クラス
        • コントローラ
          • error_handler.php
        • http_response_exception.php
        • kohana.php
      • ビュー
        • error.php

コード:

init.php:

<?php defined('SYSPATH') or die('No direct access');

Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
    ->defaults(array(
            'controller' => 'error_handler'
));

http_response_exception.php:

<?php defined('SYSPATH') or die('No direct access');

class HTTP_Response_Exception extends Kohana_Exception {


    public static function exception_handler(Exception $e)
    {

            if (Kohana::DEVELOPMENT === Kohana::$environment)
            {
                    Kohana_Core::exception_handler($e);
            }
            else
            {
                    Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

                    $attributes = array
                    (
                            'action'  => 500,
                            'message' => rawurlencode($e->getMessage()),
                    );

                    if ($e instanceof HTTP_Response_Exception)
                    {
                            $attributes['action'] = $e->getCode();
                    }

                    // Error sub-request.
                    echo Request::factory(Route::url('error', $attributes))
                            ->execute()
                            ->send_headers()
                            ->response;
            }
    }
}

kohana.php:

<?php defined('SYSPATH') or die('No direct script access.');

class Kohana extends Kohana_Core
{

    /**
     * Redirect to custom exception_handler
     */
    public static function exception_handler(Exception $e)
    {
            Error::exception_handler($e);
    }

} // End of Kohana

error_handler.php:

<?php defined('SYSPATH') or die('No direct access');

class Controller_Error_handler extends Controller {

    public function  before()
    {
            parent::before();

            $this->template = View::factory('template/useradmin');
            $this->template->content = View::factory('error');

            $this->template->page = URL::site(rawurldecode(Request::$instance->uri));

            // Internal request only!
            if (Request::$instance !== Request::$current)
            {
                    if ($message = rawurldecode($this->request->param('message')))
                    {
                            $this->template->message = $message;
                    }
            }
            else
            {
                    $this->request->action = 404;
            }
    }

    public function action_404()
    {
            $this->template->title = '404 Not Found';

            // Here we check to see if a 404 came from our website. This allows the
            // webmaster to find broken links and update them in a shorter amount of time.
            if (isset ($_SERVER['HTTP_REFERER']) AND strstr($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) !== FALSE)
            {
                    // Set a local flag so we can display different messages in our template.
                    $this->template->local = TRUE;
            }

            // HTTP Status code.
            $this->request->status = 404;
    }

    public function action_503()
    {
            $this->template->title = 'Maintenance Mode';
            $this->request->status = 503;
    }

    public function action_500()
    {
            $this->template->title = 'Internal Server Error';
            $this->request->status = 500;
    }

} // End of Error_handler

どこが間違っているのか本当にわかりません。助けてくれてありがとう。

4

5 に答える 5

2

まず、アプリケーション/ bootstrap.phpファイルのモジュールセクションにモジュールを含めることで、モジュールをロードしていることを確認する必要があります。

Kohana::modules(array(
'my'=>MODPATH.'my'
)
);

エラーハンドラコントローラのURLに直接アクセスすると、404エラーが発生するという事実から、モジュールがロードされていないと思います。

また、さらにいくつかの変更を提案します。

このクラスは例外ではなく、例外ハンドラーであるため、http_response_exception.phpはKohana_Exceptionを拡張する必要はありません。これらの同じ行に沿って、クラスは例外を表していないが、それらを処理しているため、より適切なクラス名はException_Handlerである可能性があります。次に、このファイルの名前の付け方により、modules / my / classes / http / response/exception.phpに配置する必要があります。それ以外は、このクラスのコードは問題ないように見えます。

同様に、コントローラーの名前の付け方により、コントローラーの配置と名前の付け方を少し変える必要があります。modules / my / classes / controller / error/handler.phpに移動します

http://kohanaframework.org/3.2/guide/kohana/conventionsのように、クラス名のアンダースコアは新しいディレクトリを意味することに注意してください。

最後に、ここでKohana_Coreクラスを拡張する必要はないと思いますが、代わりに独自のカスタム例外ハンドラーを登録するだけです。カスタム例外ハンドラーは、アプリケーションのブートストラップファイルまたはモジュールのinitファイルのいずれかに次の汎用コードで登録できます。

set_exception_handler(array('Exception_Handler_Class', 'handle_method'));

これが私が使用している顧客例外ハンドラーです。これはあなたのものと非常によく似ています。

<?php defined('SYSPATH') or die('No direct script access.');

class Exception_Handler {

public static function handle(Exception $e)
{
    $exception_type = strtolower(get_class($e));
    switch ($exception_type)
    {
        case 'http_exception_404':
            $response = new Response;
            $response->status(404);
            $body = Request::factory('site/404')->execute()->body();
            echo $response->body($body)->send_headers()->body();
            return TRUE;
            break;
        default:
            if (Kohana::$environment == Kohana::DEVELOPMENT)
            {
                return Kohana_Exception::handler($e);
            }
            else
            {
                Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));
                $response = new Response;
                $response->status(500);
                $body = Request::factory('site/500')->execute()->body();
                echo $response->body($body)->send_headers()->body();
                return TRUE;
            }
            break;
    }
}

}
于 2011-09-19T21:31:04.283 に答える
2

古いドキュメントを使用しています。HTTP_Exception_404 は 3.1 にバンドルされており、3.0 からソリューションを実装しようとしています。

機能するソリューションについては、お使いのバージョンの Kohana のドキュメントを参照してください。

于 2011-10-03T13:20:34.830 に答える
2

非常に長い時間の検索の後、私はついに小さな問題の解決策を見つけました。

以下は、Kohana 3.2 を使用して独自のカスタム エラー ページを読み込む方法に関するステップ バイ ステップのチュートリアルです。

  1. ブートストラップで環境変数を変更します。

ここには複数のオプションがあります。

を。bootstrap.php のドキュメントで彼らが言うことをしてください:

/**
 * Set the environment status by the domain.
 */

if (strpos($_SERVER['HTTP_HOST'], 'kohanaphp.com') !== FALSE)
{
    // We are live!
    Kohana::$environment = Kohana::PRODUCTION;

    // Turn off notices and strict errors
    error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
}

b. または、「if」なしで次の 2 行を追加するだけです。

Kohana::$environment = Kohana::PRODUCTION;
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);

c. 私はこの方法を試していませんが、新しい bootstrap.php には次のコードがあります。

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
if (isset($_SERVER['KOHANA_ENV']))
{
    Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

これらの行の前に、値「production」を「$_SERVER['KOHANA_ENV']」に指定するだけでよいと思います。

繰り返しますが、私は試していませんが、うまくいくはずです。

私は個人的に、これらのコード行をコメントアウトしました。

2 次に、「ini.php」ファイルまたは「bootstra.php」ファイルにいくつかの構成を追加する必要があります。

<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Turn errors into exceptions. 
 */
Kohana::$errors = true;

/**
 * Custom exception handler.
 */
restore_exception_handler();
set_exception_handler(array('Exception_Handler', 'handler'));

/**
 * Error route.
 */
Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
->defaults(array(
    'controller' => 'exception_handler'
));

これが欠けていて、それを難し​​くしました。残りの部分については、Kohana3.2 のドキュメントに従うか、GitHub のリポジトリに追加したモジュールを入手できます: https://github.com/jnbdz/Kohana-error

于 2012-02-04T04:16:15.943 に答える
2

あなたがする必要があるのは、bootstrap.php の追加で別のビューへのパスを設定することだけです:

Kohana_Exception::$error_view = 'error/myErrorPage';

これは、現在解析されているすべての変数を、存在するエラーページに解析します。

system/views/kohana/error.php

すなわち:

<h1>Oops [ <?= $code ?> ]</h1>
<span class="message"><?= html::chars($message) ?></span>
于 2012-02-02T02:01:30.177 に答える
0

すべてのアンダースコアは、クラス名のディレクトリ セパレータです。したがって、クラスに名前を付けるときHttp_Response_Exceptionは、クラスは にある必要がありclasses/http/response/exception.phpます。そうしないと、クラスは Kohana のオートローダーによって検出されません。

編集

うーん、この点でドキュメントが間違っているようです。classes/http_response_exception.php意味がありません。

于 2011-09-16T09:54:44.850 に答える