我正在使用Kohana 3.3,我在这里阅读forum post。
它说为了防止向最终用户显示堆栈跟踪,我需要覆盖Kohana_Exception :: _ handler()以执行与渗透错误不同的操作。这是否意味着覆盖Kohana_Exception
并添加以下功能?
public static function _handler(Exception $e)
{
try
{
// Log the exception
Kohana_Exception::log($e);
// Generate the response
//instead of below line:
//$response = Kohana_Exception::response($e);
$response = //what do I do here, return a 404 page or custom 500 page?
return $response;
}
//rest of function
}
如果是这样,我该怎么回事?
编辑:
bootstrap.php中
/**
* Attach the file write to logging. Multiple writers are supported.
*/
Kohana::$log->attach(new Log_File(APPPATH.'logs'));
/**
* Attach a file reader to config. Multiple readers are supported.
*/
Kohana::$config->attach(new Config_File);
/**
* Attach customer error handler if we are in production
*/
if(Kohana::$environment == Kohana::PRODUCTION || Kohana::$environment == Kohana::STAGING)
{
set_exception_handler(array('My_Exception', 'handler'));
throw new Exception('text'); //this works, if removed however my exception handler does not do anything
}
My_Exception.php(在classes / My / Exception.php中)
<?php
class My_Exception extends Kohana_Exception
{
public static function handler(Exception $e)
{
try
{
// Log the exception
Kohana_Exception::log($e);
// Let's try and load an error View
$class = get_class($e);
if($class == 'HTTP_Exception_404')
{
$view = View::factory('errors/404');
$view->set('error_code', 404);
}
else
{
$view = View::factory('errors/general');
$view->set('error_code', $e->getCode()); // alternatively use $e->getCode()
$view->set('error_message', $e->getMessage()); // alternatively use $e->getMessage();
}
// Let's render the output and send it to the browser
$response = $view->render();
echo $response;
}
catch (Exception $e)
{
/**
* Things are going *really* badly for us, We now have no choice
* but to bail. Hard.
*/
// Clean the output buffer if one exists
ob_get_level() AND ob_clean();
// Set the Status code to 500, and Content-Type to text/plain.
header('Content-Type: text/plain; charset='.Kohana::$charset, TRUE, 500);
// This will output the Exceptiones error message
// You may want to display something else
echo $e->getMessage();
exit(1);
}
}
}
答案 0 :(得分:8)
我实际上已经对这个问题进行了相当多的调查,并且从头开始重写了我的asnwer,因为我对Kohana在这方面的行为有了更全面的了解。
要实现你需要做的两件事:
更改默认错误视图(在APPPATH/bootstrap.php
中):
/**
* Change default error View
*/
if(Kohana::$environment == Kohana::PRODUCTION || Kohana::$environment == Kohana::STAGING)
{
Kohana_Exception::$error_view = 'errors/general';
}
注意您的模板文件必须使用与Kohana本地变量名称相同(且仅限于那些)的变量名称,即:
2.在评论中的the tutorial you linked to之后创建自定义HTTP错误页面。
按照以下步骤,您可以确保:
我已经测试了上述方法,它对我来说就像一个魅力。
答案 1 :(得分:0)
我只是在bootstrap文件中设置了Kohana_Exception :: $ error_view,并创建了相应的视图,没有其他任何必要的,所有错误都已自动重定向...