如何在zend .phtml视图中显示服务器上另一个HTML文件的内容?

时间:2013-06-04 11:21:00

标签: php zend-framework caching view

我有一个zend应用程序,它在公用文件夹中的服务器上生成并存储.html文件。它是一种缓存机制,每天在一个cron作业上运行一次。

我希望zend视图cache.phtml包含最新生成的.html文件的内容。我怎么能这样做。

假设生成的'html文件名为report.html

谢谢

2 个答案:

答案 0 :(得分:1)

我创建了一个view helper来获取缓存的内容。视图助手将包含一个简单的PHP方法来定位正确的文件,读取其内容并返回它:

class App_View_Helper_Cache extends
    extends Zend_View_Helper_Abstract
{
    public function cache()
    {
        $file = <however you figure out what the file is>;
        return file_get_contents($file);
    }
}

然后,在您的视图中,您只需回显视图助手:

<?= $this->cache() ?>

答案 1 :(得分:0)

要渲染平面HTML文件:

您无需为此创建帮助,可以使用:

<?= $this->render('/path/to/report.html') ?>

但不要使用它,请使用Zend_Cache:

但是,您应该查看Zend_Cache,您可能会发现它与您的应用程序的其余部分更加一致,从模型中的Zend Cache加载变量而不是从数据库中提取。

注意:这些说明适用于Zend Framework 1,Zend Framework 2缓存has similar capabilities,但不一样。

首先,创建缓存:

$frontendOptions = array(
   'lifetime' => 60*60*24, // cache lifetime of 24 hours
   'automatic_serialization' => true
);
$backendOptions = array(
    'cache_dir' => './tmp/' // Directory where to put the cache files
);

$cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);

然后执行此操作以在需要时获取值:

public function cacheAction(){
    ...
    if(!$result = $cache->load('daily_report')){
        $result = dailyFunction();
        $cache->save($result, 'daily_report')
    }
    $this->view->result = $result;
}

这将每天运行dailyFunction()一次(如lifetime变量中所定义)并从缓存或函数返回$result。然后你可以照常在视图中使用它。

没有cron作业,没有静态HTML文件,以及缓存的所有优点。