Lithium: the most rad php framework
Lithiumいいですね!サイトがカッコいいです。左下とか!
というわけなのでLithiumで投稿、変更、削除の流れでサンプルを作ってみました。
まず初めに結果はこれです。
http://sample.conol.jp/lithium/app_sample/sample1/posts
インストール周りについては後ほどまとめようと思います。
それでは以下大体の流れです。
作業環境
- CentOS 5.5
- PHP 5.3.3
- Lithium 0.9.5
- MongoDB 1.6.2
■ MongoDBの設定
今回はMongoDBを使用します。
/etc/mongod.confを編集します。
# ルートになる su - # 編集する vi /etc/mongod.conf # 認証を有効にする auth = true # mongodの再起動 service mongod restart
■ Lithiumの接続設定
app/config/bootstrap.phpの46行目のコメントを外します。
require __DIR__ . '/connections.php';
app/config/connections.phpを編集して、defaultという名前の接続を設定します。
use \lithium\data\Connections; Connections::add('default', array( 'type' => 'MongoDb', 'login' => 'username', 'password' => 'passwd', 'database' => 'sample', ));
■ Modelの作成
app/modelsにsample1.phpとして以下の内容のファイルを作成します。
namespace app\models; class Sample1 extends \lithium\data\Model { // モデルごとの接続設定(省略可) protected $_meta = array( 'connection' => 'default', // \lithium\data\Connections::add()で設定した接続名 'source' => 'sample1' // 使用するコレクション名 ); }
■ Controllerの作成
app/controllersにSample1Controller.phpとして以下の内容のファイルを作成します。
以下の設定ではindexへのアクセスはpostsにリダイレクトしています。
Viewで使いたい関数を変数に入れてそのままViewに渡しているのだけどもっといい方法はないのかな…
namespace app\controllers; // 使用するモデル use app\models\Sample1; class Sample1Controller extends \lithium\action\Controller { public function index() { $this->redirect(array('action' => 'posts')); } public function posts() { // sample.sample1 から last_modifiedの降順で全件取得 // 下記の場合 Sample1::all($options) とも書ける $posts = Sample1::find('all', array( 'order' => array('last_modified' => -1) )); // テキスト表示加工用関数 $format_text = function($string) { $string = htmlspecialchars((string) $string, ENT_QUOTES); return str_replace(array( " ", "\r\n", "\r", "\n" ), array( "&nbsp;","\n", "\n", "<br />" ), $string ); }; // Viewに値を渡す return compact('posts', 'format_text'); } }
■ Viewテンプレートの作成
app/viewsにsample1というフォルダを作ります。これがテンプレートを置く場所になります。
そしてその中にposts.html.phpとして以下の内容のファイルを作成します。
<ul id="posts_ctr"> <? if(isset($posts) && $posts->count()): ?> <? while($post = $posts->current()): $_id = (string) $post->_id; ?> <li id="post_<?= $_id; ?>" class="posts"> <div class="posts_text"><? echo $format_text($post->text); ?></div> <div class="posts_meta"> <span><?= date('Y-m-d H:i:s', $post->last_modified); ?></span> <span class="sep"> / </span> <span><?= date('Y-m-d H:i:s', $post->created_at); ?></span> </div> <div class="posts_action"> <span class="sep">[ </span> <span><?= $this->html->link('編集', array('action' => 'edit', 'args' => $_id)); ?></span> <span class="sep"> | </span> <span><?= $this->html->link('削除', array('action' => 'del', 'args' => $_id)); ?></span> <span class="sep"> ]</span> </div> </li> <? $posts->next(); endwhile; ?> <? else: ?> <li class="posts">投稿無いです。</li> <? endif; ?> </ul>
ぶわっと書いてしまいましたがLithiumにはhtmlヘルパーとformヘルパーがあります。使い方の詳細は後でまとめようかなと思います。
あと<?= ?>のショートタグはヘルパからの出力以外はhtmlエスケープしてくれる! …はずなんですけど環境によってはなんか機能していない気が(原因究明中)
ところで、Sample1::find(‘all’)の戻り値のオブジェクトの挙動がなんかよくわからない…
http://conol.jp/blog/archives/176
まあそれはおいといて、
■ Viewレイアウトの作成
Viewのレイアウトはデフォルトでapp/views/layouts/default.html.phpが使われます。
でも複数のレイアウトを使い分けたい時が多々あります。
今回はapp/views/layouts/sample1.html.phpというファイルを作って使ってみます。内容は以下。
<!doctype html> <html> <head> <?= $this->html->charset();?> <title>li3ple1</title> <?= $this->html->style(array('html5reset-1.4.1.css', 'sample1')); ?> <?= $this->html->script(array('sample1')); ?> <?= $this->html->link('Icon', null, array('type' => 'icon')); ?> </head> <body class="app"> <div id="container"> <div id="header"> <h1>li3ple1</h1> </div> <div id="content"> <ul id="menu"> <li><?= $this->html->link('一覧', array('controller' => 'sample1')); ?></li> <li><?= $this->html->link('追加', array('action' => 'add')); ?></li> </ul> <?php echo $this->content(); ?> </div> </div> </body> </html>
$this->content()の部分にapp/views/sample1以下のテンプレートが出力されます。
そして、Sample1のコントローラーに以下を追加、
namespace app\controllers; use app\models\Sample1; class Sample1Controller extends \lithium\action\Controller { // 全てのアクションで実行 protected function _init() { parent::_init(); $this->_render['layout'] = 'sample1'; } // 以下省略 }
わーい。
これでとりあえずhttp://example.com/sample1/postsが表示されるはずですつづく。