May 9

逆引きrhaco28: かっこいいURL(?)を使う(その2:generic.ViewsMapperを使って表示)

簡単なCRUDを行うにはgeneric.ViewsMapperをUrlsと組み合わせると便利です(実際はViewsMapperだけでかなりのことが出来ます)。

generic.ViewsMapperを使用する場合はメソッド名("method=>"...")としてread, detail, create, update, dropを指定します。またargsで最低でもテーブルモデルの指定が必須です。
(argsを指定すると指定したメソッドに引数を渡すことが出来ます)

その23のproject.xmlを使ってセットアップ後。例としてリストを表示する場合

1.read
index.php

<?php
require_once("./__init__.php");
Rhaco::import("generic.Urls");
Rhaco::import("generic.ViewsMapper");
Rhaco::import("database.DbUtil");
Rhaco::import("model.Product");
$pattern = array("^read\/?$"=>array("method"=>"read","args"=>array(new Product() /* テーブルモデル */, new Criteria() /* 検索条件 */, 0 /* オフセット */, 10 /* リミット */, null /* 表示時に呼び出されるメソッド名(指定なしならviews) */)));
$db = new DbUtil(Product::connection());
$parser = Urls::parser($pattern,$db);
$parser->write();
?>


他の場合(detail,create,update,drop)は$patternの内容を変えるだけで出来ます。以下では$patternの部分のみ示します。

またidをURLで指定する場合(例:詳細表示 http://hoge.com/detail/1
正規表現部分のグループ化「()」を使って、該当部分を囲みます。これが順に引数として指定されます。
()が2つある場合は最初の()が第1引数、次の()が第2引数と判断されます。argsで指定した引数はその後に追加されます。
2.detail
index.php
$pattern = array("^detail\/([0-9]+?)\/?$"=>array("method"=>"detail","args"=>array(new Product() /* テーブルモデル */, null /* 検索条件 */)));


3.create
$pattern = array("^create\/?$"=>array("method"=>"create","args"=>array(new Product() /* テーブルモデル */,"path/to/redirect" /* create後のリダイレクト先 */,true)));


4.update
$pattern = array("^update\/([0-9]+?)\/?$"=>array("method"=>"update","args"=>array(new Product() /* テーブルモデル */,"path/to/redirect" /* update後のリダイレクト先 */,null /* 検索条件 */,true)));


5.drop
$pattern = array("^drop\/([0-9]+?)\/?$"=>array("method"=>"drop","args"=>array(new Product() /* テーブルモデル */,"path/to/redirect" /* drop後のリダイレクト先 */, null /* 検索条件 */)));


| comment(0)

このエントリーのはてなブックマーク (-)