设置生产环境–kohana3使用手册
在将你的应用程序移入生产之前,这里有一些你要对你的应用程序需要做的事。
1 . 查看文档中的配置页。这个包括了不同环境中需要改变的大部分全局设置。通常来所,你最好在生产环境的网站上启用缓存并且关闭详细信息(Kohana::init 设置)。如果你有一些路由规则,缓存路由同样是有帮助的。
2. 在 application/bootstrap.php 设置捕捉所有异常使敏感数据不会被堆栈追踪泄露出去。
3. 打开 APC 或者某些操作码缓存工具。这个能让 PHP 自身轻松的增强性能。更复杂的系统能从操作码缓存中获得更多的性能。
/**
* 设置环境字符串的域名 (默认 'development').
*/
if ($_SERVER['SERVER_NAME'] !== 'localhost') Kohana::$environment = 'production';
/**
* 初始化Kohana基本环境
*/
$settings = array(
'base_url' => '/',
'index_file' => FALSE
);
switch (Kohana::$environment) {
case 'production':
$settings += array(
'profiling' => TRUE,
'log_errors' => FALSE,
'caching' => FALSE
);
break;
default:
$settings += array(
'profiling' => FALSE,
'log_errors' => TRUE,
'caching' => TRUE
);
break;
}
Kohana::init($settings);
/**
* 使用 PATH_INFO执行主要的请求.如果没有URI被指定,URI将被自动检测
*/
$request = Request::instance($_SERVER['PATH_INFO']);
try
{
// 尝试执行响应
$request->execute();
}
catch (Exception $e)
{
if ( Kohana::$environment == 'development' )
{
// 只是重新抛出异常
throw $e;
}
// 记录错误
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
// 创建一个404的响应
$request->status = 404;
$request->response = View::factory('template')
->set('title', '404')
->set('content', View::factory('errors/404'));
}
if ($request->send_headers()->response)
{
// 获得总共用的内存和执行时间
$total = array(
'{memory_usage}' => number_format((memory_get_peak_usage() - KOHANA_START_MEMORY) / 1024, 2).'KB',
'{execution_time}' => number_format(microtime(TRUE) - KOHANA_START_TIME, 5).' seconds');
// 将统计插入到响应中
$request->response = str_replace(array_keys($total), $total, $request->response);
}
/**
* 显示请求的响应
*/
echo $request->response;