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


How to display contents of another HTML file on server within a zend .phtml view?

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

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

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

谢谢

我将创建一个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() ?>

显示一个平面HTML文件:

你不需要为此创建一个帮助器,你可以只需使用:

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

但是不要使用这个,使用Zend_Cache:

然而,您应该查看Zend_Cache,您可能会发现从模型中的Zend缓存加载变量比从数据库中提取变量更符合应用程序的其余部分。

注意:这些指令是针对Zend Framework 1的,Zend Framework 2缓存具有类似的功能,但不相同。

首先,创建缓存:
$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文件,以及缓存的所有优点。