チュートリアル - 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

  1.  -bash-2.05b$ php -q bake.php
  2.  
  3.   ___  __  _  _  ___  __  _  _  __      __  __  _  _  ___
  4.  |    |__| |_/  |__  |__] |__| |__]    |__] |__| |_/  |__
  5.  |___ |  | | \_ |___ |    |  | |      |__] |  | | \_ |___
  6.  ---------------------------------------------------------------
  7.  
  8.  
  9.  Bake -app in /CakePHP/app (y/n)
  10.  [y] > y
  11.  
  12.  
  13.  Baking...
  14.  ---------------------------------------------------------------
  15.  Name: app
  16.  Path: /CakePHP/app
  17.  ---------------------------------------------------------------
  18.  [M]odel
  19.  [C]ontroller
  20.  [V]iew
  21.  
  22.  What would you like to Bake? (M/V/C)
  23.  > c
  24.  ---------------------------------------------------------------
  25.  Controller Bake:
  26.  ---------------------------------------------------------------
  27.  Possible Controllers based on your current database:
  28.  1. Bookmarks
  29.  2. Evaluations
  30.  
  31.  Enter a number from the list above, or type in the name of another controller. 
  32.  > 1
  33.  
  34.  Would you like bake to build your controller interactively?
  35.  Warning: Choosing no will overwrite  controller if it exist. (y/n)
  36.  [y] > y
  37.  
  38.  Would you like to use scaffolding? (y/n)
  39.  [y] > n
  40.  
  41.  Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
  42.  [n] > y
  43.  
  44.  Would you like to create the methods for admin routing? (y/n)
  45.  [n] > n
  46.  
  47.  Would you like this controller to use other models besides 'Bookmark'? (y/n)
  48.  [n] > y
  49.  
  50.  Please provide a comma separated list of the classnames of other models you'd like to use.
  51.  Example: 'Author, Article, Book' 
  52.  > Evaluation
  53.  
  54.  Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
  55.  [n] > n
  56.  
  57.  Would you like this controller to use any components? (y/n)
  58.  [n] > n
  59.  
  60.  Would you like to use Sessions? (y/n)
  61.  [y] > y
  62.  
  63.  ---------------------------------------------------------------
  64.  The following controller will be created:
  65.  ---------------------------------------------------------------
  66.  Controller Name:        Bookmarks
  67.  Uses:            Evaluation
  68.  ---------------------------------------------------------------
  69.  
  70.  Look okay? (y/n)
  71.  [y] > y
  72.  
  73.  Creating file /CakePHP/app/controllers/bookmarks_controller.php
  74.  File exists, overwrite? /CakePHP/app/controllers/bookmarks_controller.php (y/n/q):y
  75.  Wrote/CakePHP/app/controllers/bookmarks_controller.php
  76.  
  77.  Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
  78.  [y] > n

BookmarksController

  1.  -bash-2.05b$ php -q bake.php
  2.  
  3.   ___  __  _  _  ___  __  _  _  __      __  __  _  _  ___
  4.  |    |__| |_/  |__  |__] |__| |__]    |__] |__| |_/  |__
  5.  |___ |  | | \_ |___ |    |  | |      |__] |  | | \_ |___
  6.  ---------------------------------------------------------------
  7.  
  8.  
  9.  Bake -app in /CakePHP/app (y/n)
  10.  [y] > y
  11.  
  12.  
  13.  Baking...
  14.  ---------------------------------------------------------------
  15.  Name: app
  16.  Path: /CakePHP/app
  17.  ---------------------------------------------------------------
  18.  [M]odel
  19.  [C]ontroller
  20.  [V]iew
  21.  
  22.  What would you like to Bake? (M/V/C)
  23.  > v
  24.  ---------------------------------------------------------------
  25.  View Bake:
  26.  ---------------------------------------------------------------
  27.  Possible Controllers based on your current database:
  28.  1. Bookmarks
  29.  2. Evaluations
  30.  
  31.  Enter a number from the list above, or type in the name of another controller. 
  32.  > 1
  33.  
  34.  Would you like bake to build your views interactively?
  35.  Warning: Choosing no will overwrite  views if it exist. (y/n)
  36.  [y] > y
  37.  
  38.  Would you like to create some scaffolded views (index, add, view, edit) for this controller?
  39.  NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n)
  40.  [n] > y
  41.  
  42.  Would you like to create the views for admin routing? (y/n)
  43.  [n] > n
  44.  
  45.  Creating file /CakePHP/app/views/bookmarks/index.thtml
  46.  Wrote/CakePHP/app/views/bookmarks/index.thtml
  47.  
  48.  Creating file /CakePHP/app/views/bookmarks/view.thtml
  49.  Wrote/CakePHP/app/views/bookmarks/view.thtml
  50.  
  51.  Creating file /CakePHP/app/views/bookmarks/add.thtml
  52.  Wrote/CakePHP/app/views/bookmarks/add.thtml
  53.  
  54.  Creating file /CakePHP/app/views/bookmarks/edit.thtml
  55.  Wrote/CakePHP/app/views/bookmarks/edit.thtml
  56.  ---------------------------------------------------------------
  57.  
  58.  View Scaffolding Complete.

EvaluationsController

  1.  -bash-2.05b$ php -q bake.php
  2.  
  3.   ___  __  _  _  ___  __  _  _  __      __  __  _  _  ___
  4.  |    |__| |_/  |__  |__] |__| |__]    |__] |__| |_/  |__
  5.  |___ |  | | \_ |___ |    |  | |      |__] |  | | \_ |___
  6.  ---------------------------------------------------------------
  7.  
  8.  
  9.  Bake -app in /CakePHP/app (y/n)
  10.  [y] > y
  11.  
  12.  
  13.  Baking...
  14.  ---------------------------------------------------------------
  15.  Name: app
  16.  Path: /CakePHP/app
  17.  ---------------------------------------------------------------
  18.  [M]odel
  19.  [C]ontroller
  20.  [V]iew
  21.  
  22.  What would you like to Bake? (M/V/C)
  23.  > c
  24.  ---------------------------------------------------------------
  25.  Controller Bake:
  26.  ---------------------------------------------------------------
  27.  Possible Controllers based on your current database:
  28.  1. Bookmarks
  29.  2. Evaluations
  30.  
  31.  Enter a number from the list above, or type in the name of another controller. 
  32.  > 2
  33.  
  34.  Would you like bake to build your controller interactively?
  35.  Warning: Choosing no will overwrite  controller if it exist. (y/n)
  36.  [y] > y
  37.  
  38.  Would you like to use scaffolding? (y/n)
  39.  [y] > n
  40.  
  41.  Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
  42.  [n] > y
  43.  
  44.  Would you like to create the methods for admin routing? (y/n)
  45.  [n] > n
  46.  
  47.  Would you like this controller to use other models besides 'Evaluation'? (y/n)
  48.  [n] > y
  49.  
  50.  Please provide a comma separated list of the classnames of other models you'd like to use.
  51.  Example: 'Author, Article, Book' 
  52.  > Bookmark
  53.  
  54.  Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
  55.  [n] > n
  56.  
  57.  Would you like this controller to use any components? (y/n)
  58.  [n] > n
  59.  
  60.  Would you like to use Sessions? (y/n)
  61.  [y] > y
  62.  
  63.  ---------------------------------------------------------------
  64.  The following controller will be created:
  65.  ---------------------------------------------------------------
  66.  Controller Name:        Evaluations
  67.  Uses:            Bookmark
  68.  ---------------------------------------------------------------
  69.  
  70.  Look okay? (y/n)
  71.  [y] > y
  72.  
  73.  Creating file /CakePHP/app/controllers/evaluations_controller.php
  74.  File exists, overwrite? /CakePHP/app/controllers/evaluations_controller.php (y/n/q):y
  75.  Wrote/CakePHP/app/controllers/evaluations_controller.php
  76.  
  77.  Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
  78.  [y] > n

EvaluationsController

  1.  -bash-2.05b$ php -q bake.php
  2.  
  3.   ___  __  _  _  ___  __  _  _  __      __  __  _  _  ___
  4.  |    |__| |_/  |__  |__] |__| |__]    |__] |__| |_/  |__
  5.  |___ |  | | \_ |___ |    |  | |      |__] |  | | \_ |___
  6.  ---------------------------------------------------------------
  7.  
  8.  
  9.  Bake -app in /CakePHP/app (y/n)
  10.  [y] > y
  11.  
  12.  
  13.  Baking...
  14.  ---------------------------------------------------------------
  15.  Name: app
  16.  Path: /CakePHP/app
  17.  ---------------------------------------------------------------
  18.  [M]odel
  19.  [C]ontroller
  20.  [V]iew
  21.  
  22.  What would you like to Bake? (M/V/C)
  23.  > v
  24.  ---------------------------------------------------------------
  25.  View Bake:
  26.  ---------------------------------------------------------------
  27.  Possible Controllers based on your current database:
  28.  1. Bookmarks
  29.  2. Evaluations
  30.  
  31.  Enter a number from the list above, or type in the name of another controller. 
  32.  > 2
  33.  
  34.  Would you like bake to build your views interactively?
  35.  Warning: Choosing no will overwrite  views if it exist. (y/n)
  36.  [y] > y
  37.  
  38.  Would you like to create some scaffolded views (index, add, view, edit) for this controller?
  39.  NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n)
  40.  [n] > y
  41.  
  42.  Would you like to create the views for admin routing? (y/n)
  43.  [n] > n
  44.  
  45.  Creating file /CakePHP/app/views/evaluations/index.thtml
  46.  Wrote/CakePHP/app/views/evaluations/index.thtml
  47.  
  48.  Creating file /CakePHP/app/views/evaluations/view.thtml
  49.  Wrote/CakePHP/app/views/evaluations/view.thtml
  50.  
  51.  Creating file /CakePHP/app/views/evaluations/add.thtml
  52.  Wrote/CakePHP/app/views/evaluations/add.thtml
  53.  
  54.  Creating file /CakePHP/app/views/evaluations/edit.thtml
  55.  Wrote/CakePHP/app/views/evaluations/edit.thtml
  56.  ---------------------------------------------------------------
  57.  
  58.  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 の使い方」へ »

トップページへ戻る / 前のページへ戻る

back to top