cakephpでHTMLの静的生成
- fc2ブログ風に、公開ROOT直下にentry-<id>.htmlを生成
- 上書き判定機能・削除機能なし
- Postモデルについては、cakephpのチュートリアルに準じる
<?php class PublishesController extends AppController { var $name = 'Publishes';//cakephpのおまじない。先頭大文字&複数形 var $uses = array('Post');//DBのテーブルの指定 array('Category', 'Post') function index(){ Configure::write ('debug', 0); $cats = $this->Category->findAll(); $posts = $this->Post->findAll(); foreach ($posts as $post){ $this->set('post',$post); $_data = $this->render(null,null,'../posts/view'); $this->output = null; $this->__file(WWW_ROOT.'entry-'.$post[Post][id].'.html', $_data); } } /** * データをファイルに書き込む。 * cake/libs/file.php を使用 * * @param string $fileName ファイルパス * @param string $data 保存するデータ * @return boolean Success * @access public */ function __file($fileName = null,$data = null) { $file->lock = true; //ファイルロックを有効 $file = new File($fileName); if(!$file->exists()) { if($file->open('w') === false) return false; } if(!$file->writable()) return false; if ($file->open('w') === false) return false;//追記ab return $file->write($data); } } ?>
公開ROOTに「entry-12.html」などが生成される。
@TODO:カテゴリも生成したい。
@TODO:上書き判定機能・削除機能(HTMLの生成リストを参照?)