Quantcast
Channel: コノルブログ »フレームワーク
Viewing all articles
Browse latest Browse all 7

Lithiumで作ってみたその1

$
0
0

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(
				" ","\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というファイルを作って使ってみます。内容は以下。

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;?= $this-&gt;html-&gt;charset();?&gt;
	&lt;title&gt;li3ple1&lt;/title&gt;
	
	&lt;?= $this-&gt;html-&gt;style(array('html5reset-1.4.1.css', 'sample1')); ?&gt;
	&lt;?= $this-&gt;html-&gt;script(array('sample1')); ?&gt;
	
	&lt;?= $this-&gt;html-&gt;link('Icon', null, array('type' =&gt; 'icon')); ?&gt;
&lt;/head&gt;
&lt;body class=&quot;app&quot;&gt;
	&lt;div id=&quot;container&quot;&gt;
		&lt;div id=&quot;header&quot;&gt;
			&lt;h1&gt;li3ple1&lt;/h1&gt;
		&lt;/div&gt;
		&lt;div id=&quot;content&quot;&gt;
			&lt;ul id=&quot;menu&quot;&gt;
				&lt;li&gt;&lt;?= $this-&gt;html-&gt;link('一覧', array('controller' =&gt; 'sample1')); ?&gt;&lt;/li&gt;
				&lt;li&gt;&lt;?= $this-&gt;html-&gt;link('追加', array('action' =&gt; 'add')); ?&gt;&lt;/li&gt;
			&lt;/ul&gt;

			&lt;?php echo $this-&gt;content(); ?&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

$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-&gt;_render['layout'] = 'sample1';
	}

	// 以下省略

}

わーい。

これでとりあえずhttp://example.com/sample1/postsが表示されるはずですつづく。


Viewing all articles
Browse latest Browse all 7

Trending Articles