0

このドキュメントに基づいて、エラー ページにルーティングするすべてのキャッチルートを実装しました。

これが私の最後のルートですbootstrap.php

Route::set('default', '<path>', array('path' => '.+'))
    ->defaults(array(
        'controller' => 'errors',
        'action'     => '404',
    ));

ただし、存在しないページに移動しようとすると、この例外がスローされ続けます

Kohana_Exception [ 0 ]: 必要なルート パラメータが渡されませんでした: パス

セグメントをオプションにする<path>(つまり、括弧で囲む) と、homeルートが読み込まれるように見えますが、これは...

Route::set('home', '')
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

ホーム ルートが最初に定義されます。

私はそのように私の主な要求を実行します

$request = Request::instance();


try {

    // Attempt to execute the response
    $request->execute();

} catch (Exception $e) {


   if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;

    // Log the error
    Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

    // Create a 404 response
    $request->status = 404;
    $request->response = Request::factory(Route::get('default')->uri())->execute();

}

$request->send_headers();
echo $request->response;

これは、404 ヘッダーがブラウザーに送信されることを意味しますが、リクエストをすべてのキャプチャー ルートに送信すると、エラー コントローラーに設定された 404 エラーが表示されるはずです。

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

class Controller_Errors extends Controller_Base {

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

    public function action_404() {


        $this->bodyClass[] = '404';

        $this->internalView = View::factory('internal/not_found');

        $longTitle = 'Page Not Found';

        $this->titlePrefix = $longTitle;


    }
}

404 エラー ページが表示されないのはなぜですか?

4

2 に答える 2

1

私のプロジェクトでは、404 に対して特定の分離ルートを指定したことはありません。適切なルートが見つからない場合に、ルーティングによってスローされた例外をキャッチするだけです。

ここでは、Kohana で発見したすべての種類の例外をどのように処理しているかを示します。お役に立てば幸いです。

try
{
    try
    {
        $request = Request::instance();
        $request->execute();
    }
    catch (ReflectionException $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->response = Request::factory('err/404')->execute();
    }
    catch (Exception404 $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->response = Request::factory('err/404')->execute();
    }
    catch (Kohana_Request_Exception $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, 404);
        echo Request::factory('err/404')->send_headers()->execute()->response;
        exit;
    }
    catch (exception $e)
    {
        Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->status = 500;
        $request->response = Request::factory('err/500')->execute();
    }
}
catch (exception $e)
{
        if (!IN_PRODUCTION)
        {
            throw $e;
        }
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
        here is the message that everything is veeeery bad
        </body>
        </html>';
        exit;
}

例外 404:

class Kohana_Exception404 extends Kohana_Exception
{
    public function __construct($message = 'error 404', array $variables = NULL, $code = 0)
    {
        parent::__construct($message, $variables, $code);
    }
 }
于 2010-05-19T01:54:44.483 に答える
0

あ、これで分かった。

この線...

$request->response = Request::factory(Route::get('default')->uri())->execute();

の結果に対するレスポンスを設定していましたRoute::get('default')->uri()。さらに調べてみると、home上記のルートと一致する空の文字列が返されることに気付きました。私はばかげていると感じます....

于 2010-05-19T02:51:18.167 に答える