The Cookbook の CakePHP ブログチュートリアルを実践してみる(5)

10.1.7 Postsコントローラの作成

Post モデルに続いて Posts コントローラを作成します。
ターミナルから以下のコマンドを実行します。

$ cd /Users/ユーザ名/Sites/eclipse_workspace/cake_blog_tutorial/cake/console
$ ./cake bake


以下の画面が表示されます。

Welcome to CakePHP v1.2.0.7962 Console
---------------------------------------------------------------
App : app
Path: /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/app
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q) 
> C

今回はコントローラを生成したいので C を入力します。


以下の画面が表示されます。

---------------------------------------------------------------
Bake Controller
Path: /Users/ryo/Sites/eclipse_workspace/cake_blog_tutorial/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Posts
Enter a number from the list above, type in the name of another controller, or 'q' to exit  
[q] > 

現在のデータベースから選択できるコントローラが一覧表示されます。
今回は posts テーブルから生成できる Posts コントローラのみです。
1 を入力します。


以下の画面が表示されます。

---------------------------------------------------------------
Baking PostsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n) 
[y] > y

対話形式でコントローラを作成するかと聞かれています。
y を入力します。


以下の画面が表示されます。

Would you like to use scaffolding? (y/n) 
[n] > n

scaffolding 機能を利用するかと聞かれています。
n を入力します。


以下の画面が表示されます。

Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n) 
[n] > y

index(一覧) , add(追加) , view(詳細) , edit(編集) といった基本的なメソッドを生成するか聞かれています。
y を入力します。


以下の画面が表示されます。

Would you like to create the methods for admin routing? (y/n) 
[n] > n

admin routing 用のメソッドを生成するか聞かれています。
n を入力します。


以下の画面が表示されます。

Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n) 
[n] > n

HtmlHelper と FormHelper 以外のヘルパーを利用するか聞かれています。
n を入力します。


以下の画面が表示されます。

Would you like this controller to use any components? (y/n) 
[n] > n

何かコンポーネントを利用するか聞かれています。
n を入力します。


以下の画面が表示されます。

Would you like to use Sessions? (y/n) 
[y] > y

Session を利用するか聞かれています。
y を入力します。


以下の画面が表示されます。

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:  Posts

Notice: Undefined variable: helpers in /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/cake/console/libs/tasks/controller.php on line 195

Notice: Undefined variable: components in /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/cake/console/libs/tasks/controller.php on line 207
---------------------------------------------------------------
Look okay? (y/n) 
[y] > y

今まで設定したきた項目の確認です。
Notice は無視して構いません。
問題は無いので y を入力します。


以下の画面が表示されます。

Notice: Undefined variable: helpers in /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/cake/console/libs/tasks/controller.php on line 222

Notice: Undefined variable: components in /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/cake/console/libs/tasks/controller.php on line 222

Notice: Undefined variable: uses in /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/cake/console/libs/tasks/controller.php on line 222

Creating file /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/app/controllers/posts_controller.php
Wrote /Users/*****/Sites/eclipse_workspace/cake_blog_tutorial/app/controllers/posts_controller.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n) 
[y] > n

ユニットテスト用のファイルを生成するかを聞いかれています。
今回は ユニットテスト用のファイルは生成しません。
n を入力します。
Notice は無視して構いません。


もとの画面に戻ってくるので Q でBake をぬけます
生成されたファイルは以下になります。

<?php
class PostsController extends AppController {

	var $name = 'Posts';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Post.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The Post has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Post', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The Post has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->del($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>