我試圖按照此處的說明操作:http://kohanaframework.org/3.0/guide/kohana/tutorials/error-pages但由於某種原因,我無法捕獲HTTP_Exception_404我仍然收到一個醜陋的錯誤頁面,而不是我的自定義頁面。如何在Kohana中捕獲HTTP_Exception_404錯誤
此外,當我輸入URL錯誤/ 404 /消息,我得到一個醜陋的Kohana HTTP 404錯誤消息。
這裏是文件結構:
- 模塊
- 我
- 的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
我真的不明白我做錯了什麼。預先感謝您的幫助。
嗨,我用你的模塊從GitHub創建自定義錯誤,但我仍然無法使它的工作。你可以幫我嗎? –
在Stackoverflow上發佈您的問題,並將其鏈接到此處我會盡力幫助您。 – jnbdz
謝謝,這裏是[我的問題的鏈接](http://stackoverflow.com/questions/19971897/custom-error-page-cant-get-into-handler) –