日期:2014-05-17  浏览次数:20527 次

创建静态HTML文件的类和方法提议
不知道在Yii中有没有自动生成静态HTML文件的方法。这在大型门户网站内容发布中经常使用到。如果没有,建议qiang整合之,有很好的价值,比缓存还好使一些。如果有,请告示如何使用。

静态文件生成,一般通常有两种机制,一种是后台触发生成,一种是前台访问,如果没有则调用静态文件生成。这些生成的文件最好可以存储在指定的Apache文件目录或者Nginx服务目录下,使之可以用纯HTML格式访问,而无需通过PHP解释执行。

另外如果实现图片服务器分离,需要把图片传输到远程指定服务器上,不清楚有无解决方案



PHP Yii 中文群 群号:38352603中离家出走大侠的贡献方案,不过离家说比较山寨,但总比没有强,供参考和讨论,提供更多场景供qiang完善。

/**
* 生成一个html文件
* @param Book $book
* @param Chapter $chapter
* @return void
*/
public static function createHtml($book, $chapter)
{
$webAppPath = Yii::app()->params['webAppPath'];
$suffix = Yii::app()->params['staticSuffix'];
$typeId = IFileHelper::selfType($book->bookid);
$htmlFile = $webAppPath . 'read' . DS . $typeId . DS . $book->bookid . DS . $chapter->chapterid . $suffix;
$readTemplateFile = $webAppPath . 'themes' . DS . 'web' . DS . 'views' . DS . 'book' . DS . 'read.php';

if ($chapter->prevChapter($chapter->chapterid, $chapter->bookid))
{
$prevUrl = '/read/' . $typeId . '/' . $book->bookid . '/' . $chapter->prevChapter($chapter->chapterid, $chapter->bookid)->chapterid . $suffix;
}

if ($chapter->nextChapter($chapter->chapterid, $chapter->bookid))
{
$nextUrl = '/read/' . $typeId . '/' . $book->bookid . '/' . $chapter->nextChapter($chapter->chapterid, $chapter->bookid)->chapterid . $suffix;
}
$enterUrl = '/chapter/' . $book->bookid . $suffix;
$homeUrl = '/';
$showUrl = '/show/' . $book->bookid . $suffix;
$chapterUrl = '/chapter/' . $book->bookid . $suffix;

$chapterContent = $chapter->content;
ob_start();
include($readTemplateFile);
$content = ob_get_clean();
IFileHelper::createFile($htmlFile, $content);
}