チュートリアル - scaffolding からソースコードへ
scaffolding はあくまで開発をサポートする機能に過ぎません。具体的な Web システムの構築にはコーディングの作業が必要になります。そこで bake.php というスクリプトを使って scaffold と同等のソースコードを作ってみます。
bake.php は CakePHP をインストールしたディレクトリの…
└─cake
└─scripts
└ bake.php…にあります。
bake.php はスクリプトですので、コマンドラインから…
php -q ./bake.php
…と、実行します。
今回 bake.php で生成するオブジェクトは BookmarksController, BookmarksController の表示テンプレート, EvaluationsController, EvaluationsController の表示テンプレートの4つです。モデルはすでに完成系ですので生成しません。
具体的な手順です。 bake.php では bake.php からの質問→ユーザ(つまり私たち)の回答のサイクルで徐々に設定を決定します。
>
…が表示されたら、ユーザ(つまり私たち)がキーボードで回答を入力してくださいという合図です。入力後、確定するにはリターンキーを押します。
[y] >
…のように表示されることがあります。これはデフォルトが y という意味です。デフォルトのままで問題ない場合は未入力でエンターキーを押して確定することができます。
BookmarksController
- -bash-2.05b$ php -q bake.php
- ___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
- | |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
- |___ | | | \_ |___ | | | | |__] | | | \_ |___
- ---------------------------------------------------------------
- Bake -app in /CakePHP/app (y/n)
- [y] > y
- Baking...
- ---------------------------------------------------------------
- Name: app
- Path: /CakePHP/app
- ---------------------------------------------------------------
- [M]odel
- [C]ontroller
- [V]iew
- What would you like to Bake? (M/V/C)
- > c
- ---------------------------------------------------------------
- Controller Bake:
- ---------------------------------------------------------------
- Possible Controllers based on your current database:
- 1. Bookmarks
- 2. Evaluations
- Enter a number from the list above, or type in the name of another controller.
- > 1
- Would you like bake to build your controller interactively?
- Warning: Choosing no will overwrite controller if it exist. (y/n)
- [y] > y
- Would you like to use scaffolding? (y/n)
- [y] > n
- Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
- [n] > y
- Would you like to create the methods for admin routing? (y/n)
- [n] > n
- Would you like this controller to use other models besides 'Bookmark'? (y/n)
- [n] > y
- Please provide a comma separated list of the classnames of other models you'd like to use.
- Example: 'Author, Article, Book'
- > Evaluation
- Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
- [n] > n
- Would you like this controller to use any components? (y/n)
- [n] > n
- Would you like to use Sessions? (y/n)
- [y] > y
- ---------------------------------------------------------------
- The following controller will be created:
- ---------------------------------------------------------------
- Controller Name: Bookmarks
- Uses: Evaluation
- ---------------------------------------------------------------
- Look okay? (y/n)
- [y] > y
- Creating file /CakePHP/app/controllers/bookmarks_controller.php
- File exists, overwrite? /CakePHP/app/controllers/bookmarks_controller.php (y/n/q):y
- Wrote/CakePHP/app/controllers/bookmarks_controller.php
- Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
- [y] > n
BookmarksController
- -bash-2.05b$ php -q bake.php
- ___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
- | |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
- |___ | | | \_ |___ | | | | |__] | | | \_ |___
- ---------------------------------------------------------------
- Bake -app in /CakePHP/app (y/n)
- [y] > y
- Baking...
- ---------------------------------------------------------------
- Name: app
- Path: /CakePHP/app
- ---------------------------------------------------------------
- [M]odel
- [C]ontroller
- [V]iew
- What would you like to Bake? (M/V/C)
- > v
- ---------------------------------------------------------------
- View Bake:
- ---------------------------------------------------------------
- Possible Controllers based on your current database:
- 1. Bookmarks
- 2. Evaluations
- Enter a number from the list above, or type in the name of another controller.
- > 1
- Would you like bake to build your views interactively?
- Warning: Choosing no will overwrite views if it exist. (y/n)
- [y] > y
- Would you like to create some scaffolded 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)
- [n] > y
- Would you like to create the views for admin routing? (y/n)
- [n] > n
- Creating file /CakePHP/app/views/bookmarks/index.thtml
- Wrote/CakePHP/app/views/bookmarks/index.thtml
- Creating file /CakePHP/app/views/bookmarks/view.thtml
- Wrote/CakePHP/app/views/bookmarks/view.thtml
- Creating file /CakePHP/app/views/bookmarks/add.thtml
- Wrote/CakePHP/app/views/bookmarks/add.thtml
- Creating file /CakePHP/app/views/bookmarks/edit.thtml
- Wrote/CakePHP/app/views/bookmarks/edit.thtml
- ---------------------------------------------------------------
- View Scaffolding Complete.
EvaluationsController
- -bash-2.05b$ php -q bake.php
- ___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
- | |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
- |___ | | | \_ |___ | | | | |__] | | | \_ |___
- ---------------------------------------------------------------
- Bake -app in /CakePHP/app (y/n)
- [y] > y
- Baking...
- ---------------------------------------------------------------
- Name: app
- Path: /CakePHP/app
- ---------------------------------------------------------------
- [M]odel
- [C]ontroller
- [V]iew
- What would you like to Bake? (M/V/C)
- > c
- ---------------------------------------------------------------
- Controller Bake:
- ---------------------------------------------------------------
- Possible Controllers based on your current database:
- 1. Bookmarks
- 2. Evaluations
- Enter a number from the list above, or type in the name of another controller.
- > 2
- Would you like bake to build your controller interactively?
- Warning: Choosing no will overwrite controller if it exist. (y/n)
- [y] > y
- Would you like to use scaffolding? (y/n)
- [y] > n
- Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
- [n] > y
- Would you like to create the methods for admin routing? (y/n)
- [n] > n
- Would you like this controller to use other models besides 'Evaluation'? (y/n)
- [n] > y
- Please provide a comma separated list of the classnames of other models you'd like to use.
- Example: 'Author, Article, Book'
- > Bookmark
- Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
- [n] > n
- Would you like this controller to use any components? (y/n)
- [n] > n
- Would you like to use Sessions? (y/n)
- [y] > y
- ---------------------------------------------------------------
- The following controller will be created:
- ---------------------------------------------------------------
- Controller Name: Evaluations
- Uses: Bookmark
- ---------------------------------------------------------------
- Look okay? (y/n)
- [y] > y
- Creating file /CakePHP/app/controllers/evaluations_controller.php
- File exists, overwrite? /CakePHP/app/controllers/evaluations_controller.php (y/n/q):y
- Wrote/CakePHP/app/controllers/evaluations_controller.php
- Cake test suite not installed. Do you want to bake unit test files anyway? (y/n)
- [y] > n
EvaluationsController
- -bash-2.05b$ php -q bake.php
- ___ __ _ _ ___ __ _ _ __ __ __ _ _ ___
- | |__| |_/ |__ |__] |__| |__] |__] |__| |_/ |__
- |___ | | | \_ |___ | | | | |__] | | | \_ |___
- ---------------------------------------------------------------
- Bake -app in /CakePHP/app (y/n)
- [y] > y
- Baking...
- ---------------------------------------------------------------
- Name: app
- Path: /CakePHP/app
- ---------------------------------------------------------------
- [M]odel
- [C]ontroller
- [V]iew
- What would you like to Bake? (M/V/C)
- > v
- ---------------------------------------------------------------
- View Bake:
- ---------------------------------------------------------------
- Possible Controllers based on your current database:
- 1. Bookmarks
- 2. Evaluations
- Enter a number from the list above, or type in the name of another controller.
- > 2
- Would you like bake to build your views interactively?
- Warning: Choosing no will overwrite views if it exist. (y/n)
- [y] > y
- Would you like to create some scaffolded 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)
- [n] > y
- Would you like to create the views for admin routing? (y/n)
- [n] > n
- Creating file /CakePHP/app/views/evaluations/index.thtml
- Wrote/CakePHP/app/views/evaluations/index.thtml
- Creating file /CakePHP/app/views/evaluations/view.thtml
- Wrote/CakePHP/app/views/evaluations/view.thtml
- Creating file /CakePHP/app/views/evaluations/add.thtml
- Wrote/CakePHP/app/views/evaluations/add.thtml
- Creating file /CakePHP/app/views/evaluations/edit.thtml
- Wrote/CakePHP/app/views/evaluations/edit.thtml
- ---------------------------------------------------------------
- View Scaffolding Complete.
これら bake.php によって新規追加(コントローラの場合は更新)されるのは次のファイルです。
└─app
├─controllers
│ bookmarks_controller.php
│ evaluations_controller.php
│
└─views
├─bookmarks
│ add.thtml
│ edit.thtml
│ index.thtml
│ view.thtml
│
└─evaluations
add.thtml
edit.thtml
index.thtml
view.thtml/app/controllers/bookmarks_controller.php を開いてください。 bake.php によって追加されたソースコードが確認できます。
/app/controllers/bookmarks_controller.php
<?php
class BookmarksController extends AppController {
var $name = 'Bookmarks';
var $uses = array('Bookmark', 'Evaluation');
var $helpers = array('Html', 'Form' );
function index() {
$this->Bookmark->recursive = 0;
$this->set('bookmarks', $this->Bookmark->findAll());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid id for Bookmark.');
$this->redirect('/bookmarks/index');
}
$this->set('bookmark', $this->Bookmark->read(null, $id));
}
function add() {
if (empty($this->data)) {
$this->render();
} else {
$this->cleanUpFields();
if ($this->Bookmark->save($this->data)) {
$this->Session->setFlash('The Bookmark has been saved');
$this->redirect('/bookmarks/index');
} else {
$this->Session->setFlash('Please correct errors below.');
}
}
}
function edit($id = null) {
if (empty($this->data)) {
if (!$id) {
$this->Session->setFlash('Invalid id for Bookmark');
$this->redirect('/bookmarks/index');
}
$this->data = $this->Bookmark->read(null, $id);
} else {
$this->cleanUpFields();
if ($this->Bookmark->save($this->data)) {
$this->Session->setFlash('The Bookmark has been saved');
$this->redirect('/bookmarks/index');
} else {
$this->Session->setFlash('Please correct errors below.');
}
}
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid id for Bookmark');
$this->redirect('/bookmarks/index');
}
if ($this->Bookmark->del($id)) {
$this->Session->setFlash('The Bookmark deleted: id '.$id.'');
$this->redirect('/bookmarks/index');
}
}
}
?>これは scaffolding と同等のソースです。
アクションである各メソッド index, view, add, edit, delete に注目してください。その名前に対応する *.thtml ファイルが /app/views/bookmarks/ 以下に保存されています。
└─app
└─views
├─bookmarks
│ add.thtml
│ edit.thtml
│ index.thtml
│ view.thtml
│
└─evaluations
add.thtml
edit.thtml
index.thtml
view.thtmlこれは HTML のテンプレートファイルですが、すべて PHP で書かれていますので直接編集することができ、 PHP の知識があれば思い通りのデザインのページを作ることができます。
« 「チュートリアル - 入力データのチェック」へ | トップへ | 上の階層へ | 「より実践的な CakePHP の使い方」へ »