nazolabo

フリーランスのWebエンジニアが近況や思ったことを発信しています。

rhaco2を使う

rev4574だよ!1ヶ月後くらいには別物になってるかもしれないから気をつけてね!
参考:http://www.sharkpp.net/php/rhaco/how-to-use-rhaco2.html

初期設定

  • __settings__.phpを作る
<?php
require_once("/path/to/rhaco2/rhaco2"); // rhaco2のRhaco.phpを読み込む
application_settings(__FILE__, 'http://localhost');

application_settingsはRhaco::initのエイリアス
initは

static public function init($path,$url=null,$lib=null,$work=null){

ということで、

  • 自分のパス
  • このアプリケーションのURL

を指定する。

とりあえず何か出す

<?php
include_once(dirname(__FILE__)."/__settings__.php");
import('core.Flow');

$flow = new Flow();
$flow->output('index.html');
  • templates/index.html
<html>
<body>
hoge
</body>
</html>

url handlerを使う

<?php

include_once(dirname(__FILE__)."/__settings__.php");
import('core.Flow');

$flow = new Flow();
$flow->handler(array(
    ""=>"class=BlogView,method=index,template=index.html,name=blog",
));
$flow->output();

$flow->handlerで、URLと処理対象のマッピングを書く
Cakeとかみたいに書いてある通りに動くと思うな!このゆとりが!全部マッピング書け!
いや多分Cakeみたいに指定箇所に置けば動くようにもできると思うけど
keyのURLマッピング(ここでは空なので全部)にマッチしたら、classのmethodをController的に呼び出して(MTVと考えればViewか)、index.htmlを表示する。nameは識別用としてしか使われてない?後で使う?

  • lib/BlogView.php
<?php
class BlogView extends Flow {
    public function index()
    {
        $this->vars('hoge', 'fuga');

        return $this;
    }
}

普通のControllerっぽいクラス。Flowを継承する。
$this->varsでテンプレート変数を割り当てる。

  • templates/index.html
<html>
<body>
{$hoge}
</body>
</html>

{$hoge}でアサインした変数の出力

DB使いたい!

  • テーブル作成
create table blog (
  id int not null auto_increment primary key,
  name varchar(255),
  title varchar(255),
  body text,
  created datetime,
  updated timestamp
);
insert into blog(name, title, body, created) values('nazo', 'title1', 'body1', now());
insert into blog(name, title, body, created) values('nazo', 'title2', 'body2', now());
insert into blog(name, title, body, created) values('nazo', 'title3', 'body3', now());
  • lib/model/Blog.php
<?php
import('db.Dao');

class Blog extends Dao
{
    static protected $__id__ = "type=serial";
    static protected $__name__ = "type=string";
    static protected $__title__ = "type=string";
    static protected $__body__ = "type=string";
    static protected $__created__ = "type=timestamp";
    static protected $__updated__ = "type=timestamp";

    protected $id;
    protected $name;
    protected $title;
    protected $body;
    protected $created;
    protected $updated;

    protected function __init__(){
        $this->created = time();
        $this->updated = time();
    }
}

※型訂正

  • __settings__.php
<?php
require_once("/path/to/rhaco2/rhaco2");
application_settings(__FILE__,"http://localhost");
def("db.Db@app","type=mysql,dbname=rhaco2test,host=localhost,user=root,password=");
  • lib/BlogView.php
<?php
import('model.Blog');

class BlogView extends Flow {

    public function index()
    {
        $this->vars('entries', C('Blog')->find_all());

        return $this;
    }

}
  • templates/index.html
<html>
    <body>
        <ul rt:param="entries" rt:var="entry" rt:reverse="true">
            <li><span>{$t.html($entry.name())}</span><span> [{$entry.fmCreated()}]</span></li>
            <li><span>{$t.html($entry.title())}</span></li>
            <li><span>{$t.html($entry.body())}</span></li>
        </ul>
    </body>
</html>