創建靜態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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章