前回解説したViewsをつかうと、
今回は、
Urlsとは
CakePHPなど、
正規表現を使って、
Urlsをつかう
使い方は非常に簡単です。Urlsクラスのparserメソッドに、
使用例
ここでは、
<?php
require_once '__init__.php';
Rhaco::import('generic.Urls');
Rhaco::import('model.Todo');
$patterns = array(
// リスト
'^$' => array(
'class' => 'generic.Views',
'method' => 'read',
'args' => array(new Todo(), new C(Q::eq(Todo::columnClose(), false))),
'template' => 'list.html',
),
// generic.Views を使用する場合は、設定を省略できます。
'^detail/(\d+)$' => array('method' => 'detail', 'args' => array(new Todo(), new C(Q::fact()))),
'^create$' => array('method' => 'create', 'args' => array(new Todo(), null, Rhaco::url())),
'^update/(\d+)$' => array('method' => 'update', 'args' => array(new Todo(), null, Rhaco::url())),
'^delete/(\d+)$' => array('method' => 'delete', 'args' => array(new Todo(), null, Rhaco::url())),
);
$parser = Urls::parser($patterns);
$parser->write();
Urlsで指定できる値は次の通りです。
値 | 意味 |
---|---|
class | 実行するメソッドを持つクラス |
method | 実行するメソッド名 |
args | メソッドに渡す引数 |
template | テンプレート名 |
var | 追加でテンプレートにセットする値 |
この状態でindex.
つぎに、
setup/
<project rhacover="1.6.1" version="0.0.1" name="kaeru" ...
<project rhacover="1.6.1" version="0.0.1" name="kaeru" pathinfo="index.php" ...
変更後、

このボタンをクリックすると、
RewriteEngine On
RewriteBase /kaeru/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?%{QUERY_STRING}&pathinfo=$1 [L]
完了ボタンを作成する
作成したTODOを完了するためには、
先に述べたUrlsの指定にupdateの項目が既にあるので、
<table id="todo" rt:param="object_list" rt:var="object">
<tbody>
<tr class="even">
<td>{$object.factCategory.toString()}</td>
<td><a href="{$rhaco.url('detail')}/{$object.id}">{$object.subject}</a></td>
<td>{$f.text2html($object.description)}</td>
<td>{$object.captionPriority()}</td>
<form action="{$rhaco.url('update')}/{$object.id}" method="post">
<td>
<input type="hidden" name="category" value="{$object.category}" />
<input type="hidden" name="subject" value="{$object.subject}" />
<input type="hidden" name="description" value="{$object.description}" />
<input type="hidden" name="priority" value="{$object.priority}" />
<input type="hidden" name="close" value="1" />
<input type="submit" value="完了" />
</td>
</form>
</tr>
</tbody>
このように、
Criteriaでリレーションを扱う
Q::fact()
参照関係のあるデータを取得するとき、
例えば、
// リスト
'^$' => array(
'class' => 'generic.Views',
'method' => 'read',
'args' => array(new Todo(), new C(Q::eq(Todo::columnClose(), false), Q::fact())),
'template' => 'list.html',
),
Q::depend()
参照関係のあるデータを取得するとき、
例えば、
'^cat/(\d+)$' => array(
'method' => 'detail',
'args' => array(new Category(), new C(Q::depend()))
),
<rt:extends href="../list.html" />
<rt:block name="content">
<h2>{$object.name}</h2>
<p><a href="{$rhaco.url('create')}">Add new todo</a></p>
<table id="todo" rt:param="{$object.dependTodos}" rt:var="todo">
(以下省略)
まとめ
このように、
参考資料
これまで開発してきたアプリケーションのソースコードをgithub上にアップロードしました。多少発展的なコードも含めてみたので、
gitクライアントからのアクセスのほか、