1. 程式人生 > >tp5 File檔案寫入(生成靜態檔案)

tp5 File檔案寫入(生成靜態檔案)

利用\think\template\driver\File.php檔案整合buildHtml
1.在controller.php建立方法如下
/**
* 建立靜態頁面
* @access protected
* @htmlfile 生成的靜態檔名稱
* @htmlpath 生成的靜態檔案路徑
* @param string $templateFile 指定要呼叫的模板檔案
* 預設為空 由系統自動定位模板檔案
* @return string
*
*/
protected function buildHtml($htmlfile = '', $htmlpath = '', $templateFile = '')
{
$content = $this->fetch($templateFile);
$htmlpath = !empty($htmlpath) ? $htmlpath : './appTemplate/';
$htmlfile = $htmlpath . $htmlfile . '.'.config('url_html_suffix');

$File = new \think\template\driver\File();
$File->write($htmlfile, $content);
return $content;
}

2.需要用到的地方直接呼叫
$this->buildHtml($id,APP_PATH.'app/view/content/',APP_PATH.'app/tpl/activity.html');