2010-05-19 4 views
0

을 기반으로, 모두를 캐치하여 오류 페이지로 연결되는 경로를 구현했습니다. 여기 Kohana 3에 대한 지원이 필요하고 404 오류로 바뀌는 모든 경로를 잡으십시오.

은 마지막 경로 내 bootstrap.php 나는 시도하고 존재하지 않는 페이지

Kohana_Exception [0]에 갈 때 내가 던진이 예외가 계속하지만

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

: 필수 경로 매개 변수가 전달되지 않음 : 경로

<path> 세그먼트를 선택적으로 만들면 즉

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

홈 경로는 첫번째 정의 ... 다음 그냥있는 home 경로를로드하는 것) 괄호에 포장. 모든 경로는 다음이 404 오류 세트를 표시해야합니다

나는이 404 헤더가 브라우저로 전송되는 것을 의미하므로

$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; 

처럼 내 주요 요청을 실행,하지만 난 캡처에 요청을 전송하여 가정 내 오류 컨트롤러에서.

<?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 오류 페이지가 표시되지 않습니까?

답변

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; 
} 

Exception404 :

class Kohana_Exception404 extends Kohana_Exception 
{ 
    public function __construct($message = 'error 404', array $variables = NULL, $code = 0) 
    { 
     parent::__construct($message, $variables, $code); 
    } 
} 
+0

Exception404를 만들었습니까? 아니면 Kohana가 정의한 것입니까? 귀하의 답변도 주셔서 감사합니다 :) – alex

+0

이것은 내 것입니다. 나는 그 코드를 대답에 추가했다 – zerkms

+0

감사합니다 힙! 이것은 재미 있고 도움이되므로 받아 들일 것입니다. – alex

0

아, 내가이 일을 알아 냈다.

이 줄은 ...

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

Route::get('default')->uri()의 결과에 대한 응답을 설정했다. 더 자세히 살펴보면 위의 home 경로와 일치하는 빈 문자열이 반환된다는 것을 깨달았습니다. 나는 어리석은 것을 느낍니다 ....

관련 문제