Zend_Cache_Frontend_Page无法识别没有echo的输出

时间:2012-01-09 20:03:30

标签: zend-framework

我正在尝试使用Zend_Cache,所以我将以下代码添加到我的操作中(稍后将移至bootstrap):

$frontendOptions = array(
    'lifetime' => 7200,
    'debug_header' => true, // für das Debuggen
    'default_options' => array(
        'cache' => true,
        'cache_with_get_variables' => true,
        'cache_with_session_variables' => true,
        'cache_with_cookie_variables' => true,
        'cache_with_post_variables' => true,
    )
);

$backendOptions = array(
    'cache_dir' => '/tmp/'
);

$cache = Zend_Cache::factory('Page', 'File',
    $frontendOptions, $backendOptions
);
echo "hej";
var_dump($cache->start('someid'));

Zend现在生成一个包含hejbool(false)的缓存文件,但除此之外它不会缓存我的页面。根据一本关于​​zend框架的德国书籍,当没有可用缓存时, false 是正确的。只有在找到缓存时才返回true。

当我在Zend_Cache_Frontend_Page.php内直接调试时,它会一直到start() - 方法的底部,这意味着没有出错(给定id)并且没有找到缓存,因此必须生成一个缓存。这已经完成(我可以在/tmp/中看到它),但没有所需的内容。

那么为什么不缓存Zend_View的输出,而只是通过echo直接输出?

我没有调用任何显式函数来渲染视图,但这似乎不再需要了(我的视图总是根据控制器和动作自动渲染)。我尝试了标准的XHTML模板(index.phtml)和RSS模板(index.rss.phtml)。

有什么想法吗?你还需要其他代码片段吗?

1 个答案:

答案 0 :(得分:1)

使用Zend_Cache_Frontend_Page时,您必须启用disableOutputBuffering选项。原因是Zend_Cache_Frontend_Page使用ob_start进行回调,它必须是对ob_start的第一次调用,否则会导致您遇到的奇怪行为。

要启用它,您可以使用

在Bootstrap中设置它
Zend_Controller_Front::getInstance()->setParam('disableOutputBuffering', true);

或在frontController-setup之后使用配置文件(这里是INI风格的配置):

resources.frontController.params.disableOutputBuffering = true