agilephp @ ウィキ
-
イテレーションA4:もっと美しく
ここでビューを変更する必要ができてきました。scaffoldのままでビューをカスタマイズするのは現実的ではなさそうなのでbakeを使用してファイルを生成することにします。
adminコントローラーに変わってProductsコントローラを生成します。管理用のコントローラなので一般用のcrudメソッドは作成せず、admin用のメソッドを生成していることに注目してください。
コントローラを生成するまえにapp/config/core.phpにある次の一行のコメントを外して有効にしてください。
Configure::write('Routing.admin', 'admin')
これにより、Productsコントローラーは /admin/product/ というパスでアクセスできるようになります。ちなみに、/products/というパスからはbakeの指定でcrudに対応するメソッド、ビューを生成しないのでアクセスできません。ちなみに、/admin/products/からアクセスされるコントローラ内のメソッドは admin_ から名前が始まり、/products/からアクセスされるメソッドとは区別されています。
pot/cake/console/cake -app ./app bake controller
Welcome to CakePHP v1.3.0.0 Console
---------------------------------------------------------------
App : app
Path: /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app
---------------------------------------------------------------
---------------------------------------------------------------
Bake Controller
Path: /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/controllers/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Controllers based on your current database:
1. Products
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 1
---------------------------------------------------------------
Baking ProductsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] >
Would you like to use dynamic scaffolding? (y/n)
[n] > n
Would you like to create some basic class methods
(index(), add(), view(), edit())? (y/n)
[n] > n
Would you like to create the basic class methods for admin routing? (y/n)
[n] > y
Would you like this controller to use other helpers
besides HtmlHelper and FormHelper? (y/n)
[n] >
Would you like this controller to use any components? (y/n)
[n] >
Would you like to use Session flash messages? (y/n)
[y] >
---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:
Products
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/controllers/products_controller.php
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/controllers/products_controller.php
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] > y
You can download SimpleTest from http://simpletest.org
Bake is detecting possible fixtures..
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/tests/cases/controllers /products_controller.test.php
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/tests/cases/controllers/products_ controller.test.php
つぎに viewを生成します。ここでもadminようのビューのみを生成し、一般のCRUD向けビューは生成しないことに注意してください。
pot/cake/console/cake -app ./app bake view
Welcome to CakePHP v1.3.0.0 Console
---------------------------------------------------------------
App : app
Path: /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app
---------------------------------------------------------------
---------------------------------------------------------------
Bake View
Path: /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Controllers based on your current database:
1. Products
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 1
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite Products views if it exist. (y/n)
[n] > y
Would you like to create some CRUD views
(index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller
and model classes (including associated models). (y/n)
[y] > n
Would you like to create the views for admin routing? (y/n)
[n] > y
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_index.ctp
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_index.ctp
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_view.ctp
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_view.ctp
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_add.ctp
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_add.ctp
Creating file /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_edit.ctp
Wrote /Users/fujisawatakeshi/opt/apache2/htdocs/depot/./app/views/products/admin_edit.ctp
---------------------------------------------------------------
View Scaffolding Complete.
/admin/products/で以前と同じビューが表示されれば成功です。 adminコントローラはこれで不要になりますので削除しておきましょう。
デフォルトのテンプレートは cake/libs/view/layouts/default.ctp になります。これを自分のアプリケーションのディクレクトリ app/views/layouts/にコピーして修正を行います。
app/webroot/css/scaffold.cssを作成してスタイルの定義を行います。
.products table{
border: none;
}
.products table tr td {
text-align:left;
border: none;
background: none;
}
.ListTitle {
color: #244;
font-weight: bold;
font-size: large;
}
.ListActions {
font-size: x-small;
text-align: right;
padding-left: 1em;
}
.ListLine0 {
background: #e0f8f8;
}
.ListLine1 {
background: #f8b0f8;
}
default.ctpの中ののなかに echo $html->css('cake.generic');という記述がありますので、その下に今作成したscaffold.cssを読み込ませるためのコードを追加します。
echo $html->css('scaffold');
コントローラにビューで使用するヘルパーを定義ます。Productsコントローラーに$helpersが定義されていますので、これに Text,Number,Timeを追加します。
app/controllers/products_controller.php
var $helpers = array('Html', 'Form','Text','Number','Time');
P70 ,P71のデザイン、レイアウトのあわせて修正するとビュー(app/views/products/admin_index.ctp)はこうなります。
商品一覧
http://www21.atwiki.jp/agilephp/pages/17.html
agilephp @ ウィキ
-
11.1イテレーションF1:ユーザーの追加
まずは、ユーザを登録するデータベーステーブルを作成します。
app/db/create.spl
drop table if exists users ;
create table users(
id int not null auto_increment,
name varchar(100) not null,
hashed_password char(40) null,
primary key (id)
);
%mysql -u root -p depot_development < app/db/create.sql
次に対応するモデルを作成します。今回はbakeを使わない方が簡単なので、手入力で作成します。
app/models/user.php
**コントローラーの作成
新規にloginコントローラをつくってユーザ管理に関する機能を集約します。
% cake/console/cake -app ./app bake controller
Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/depot/cake/console/libs/shell.php on line 156
Welcome to CakePHP v1.2.4.8284 Console
---------------------------------------------------------------
App : app
Path: /Users/fujisawa/MAMP/htdocs/depot/./app
---------------------------------------------------------------
---------------------------------------------------------------
Bake Controller
Path: /Users/fujisawa/MAMP/htdocs/depot/./app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. LineItems
2. Orders
3. Products
4. Users
Enter a number from the list above, type in the name of another controller, or 'q' to exit
[q] > Login
---------------------------------------------------------------
Baking LoginController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] > n
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[y] > n
---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name: Login
---------------------------------------------------------------
Look okay? (y/n)
[y] >
Creating file /Users/fujisawa/MAMP/htdocs/depot/./app/controllers/login_controller.php
Wrote /Users/fujisawa/MAMP/htdocs/depot/./app/controllers/login_controller.php
Baking unit test for Login...
Creating file /Users/fujisawa/MAMP/htdocs/depot/./app/tests/cases/controllers/login_controller.test.php
Wrote /Users/fujisawa/MAMP/htdocs/depot/./app/tests/cases/controllers/login_controller.test.php
login コントローラにはユーザ管理に必要なアクション(ログイン、ユーザ一覧、削除、追加)を実装します
**ユーザの追加
ユーザを追加する機能(add_userアクション)はadd_user()メソッドで実装します。add_userメソッドでは入力パラメータの有無でフォームの表示なのかデータの追加なのかを判断します。
app/controllers/login_controller.php
class LoginController extends AppController {
var $name = 'Login';
var $uses = array('User');
var $helpers = array('Html', 'Form');
function admin_add_user(){
$this->pageTitle = 'ユーザの追加';
if (empty($this->data)){
// フォームを表示
}else{
if( $this->User->save($this->data) ){
$this->Session->setFlash("ユーザ $this->data['User']['name'] が作成されました");
$this->redirect('/admin/login/add_user');
}
}
}
}
対応するビュー app/views/login/admin_add_user.ctp を作成します。
app/views/login/admin_add_user.ctp
$form->password() を使うとinput()で出力されるが出力されません。ここではinput()で統一することにします。
**モデルの修正
usersテーブルにデータを格納するまえにユーザが入力したパスワードをハッシュ文字列に変換しなくてはいけませんし、入力チェクも必要です。ハッシュへの変換は beforeSave() コールバックを使います。
app/modesl/user.php
これでデータベースに追加される前にパスワードがハッシュ値に変換されます。実際にデータを追加してデータベースにユーザが追加されていれば正常動作しています。
http://www21.atwiki.jp/agilephp/pages/33.html
agilephp @ ウィキ
-
10.1イテレーションE1:基本的な発送処理
発送日がセットされているかどうかでその商品の発送ステータスを判断するので、ordersテーブルに発送日を格納する列 shipped_atを追加します。
app/db/create.sql
create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
shipped_at datetime null,
primary key(id)
);
% mysql -u root -p depot_development < app/db/create.sql
**OrderとProductのリレーションの設定
発送処理ではリクエストがあったらまずオーダーごとに発送待ちの注文の商品の商品タイトルの一覧を表示します。そのためにはProductモデルの属性であるタイトルをしゅとくするひつようがあります。ただ、railsでは li.product.title をLineItem経由で取得できていますが、cakePHPでは line_items belongs to product の関係では LineItemモデル経由でProductモデルの属性は取得できないようです。したがって、OrderモデルとProductモデルをHABTMで関連づけます。(ほかにいい方法があるかも。。)
app/modesl/order.php
var $hasAndBelongsToMany = array(
'Products' =>
array(
'className' => 'Products',
'joinTable' => 'line_items',
'foreignKey' => 'order_id',
'associationForeignKey' => 'product_id',
)
);
**shipメソッドの作成
まずは発送待ちの注文の一覧を表示するためのアクションshipを実装します。 products コントローラにadmin_ship()メソッドを実装します。(ship()ではないので注意してください)
app/controllers/product_controller.php
function admin_ship(){
$pending_orders = $this->Order->findAllByShippedAt(NULL);
$this->set('pending_orders',$pending_orders);
}
rails のほうではモデルに pending_shippingというメソッドを追加して shipped_atがNULLのデータのみ返すようにしていますが、cakeではコントローラー内で
http://www21.atwiki.jp/agilephp/pages/31.html