かめのはこ

とあるエンジニアのメモ帳です

Lithiumしてみる

phpの軽量フレームワーク
Lithiumを試してみる

デフォがmongoDBだったのでなかなか素敵です

インストールとかは http://lithify.me/ ここから

チュートリアルがあったので確認してきます

app/config/bootstrap/connections.php

Connections::add('default', array(
  'type' => 'MongoDb',
  'host' => 'localhost',
  'port' => '27017',
  'database' => 'my_app',
));

 
おぉMongoさんですね

コントローラー
app/controllers/PostsController.php

namespace app\controllers;
use app\models\Post;
class PostsController extends \lithium\action\Controller {
    public function index(){
        $posts = Post::all();
        return compact('posts');
    }
    public function add(){
        $success = false;
        if($this->request->data){
            $post = Post::create($this->request->data);
            $success = $post->save();
        }
        return compact('success');
    }
}

app/models/Post.php

namespace app\models;
class Post extends \lithium\data\Model {
}

app/views/posts/add.html.php

<? if($success): ?>
<h1>Post Successfully Saved</h1>
<? endif; ?>
<?= $this->form->create(); ?>
    <?= $this->form->field('title');?>
    <?= $this->form->field('body', array('type' => 'textarea'));?>
    <?= $this->form->submit('Add Post'); ?>
<?= $this->form->end(); ?>
<?= $this->html->link('index','/posts/index'); ?>

app/views/posts/index.html.php

<? foreach($posts as $post): ?>
    <article>
        <h1><?= $post->title ?></h1>
        <p><?= $post->body ?></p>
    </article>
<? endforeach; ?>
<?= $this->html->link('add','/posts/add'); ?>

いじりがいはありそうだ