<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>WIDGET-INFO &#187; bake</title>
	<atom:link href="http://blog.widget-info.net/category/cakephp/cakephp_bake/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.widget-info.net</link>
	<description>WIDGET-INFOではCakePHPやIPhone情報、その他開発に関する情報を発信</description>
	<lastBuildDate>Sat, 24 Dec 2011 20:47:30 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.widget-info.net/category/cakephp/cakephp_bake/feed/" />
		<item>
		<title>CakePHP　Bake後の出力をカスタマイズ[その2]</title>
		<link>http://blog.widget-info.net/2010/11/cakephp-bake-2/</link>
		<comments>http://blog.widget-info.net/2010/11/cakephp-bake-2/#comments</comments>
		<pubDate>Sun, 14 Nov 2010 15:05:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[bake]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.widget-info.net/?p=436</guid>
		<description><![CDATA[Bake時に出力されるファイルのカスタマイズ第二弾。またまた備忘録です。]]></description>
			<content:encoded><![CDATA[<p>Bake時に出力されるファイルのカスタマイズ第二弾（笑<br />
またまた備忘録です。</p>
<p>前回は基本の部分を説明しました。<br />
個人的には毎回使うヘルパーやコンポーネントを入れたりもしますが、<br />
一番は「app_controller.php」や「app_model.php」でしょうか。<br />
<span id="more-436"></span><br />
CakePHPにはある程度の機能はありますが、全ての機能が備わっているわけでもないですし<br />
開発には必ず記述していることもあると思います。<br />
そこに時間を費やしては無駄になってしまうので、あらかじめ利用するであろうコードを<br />
記述しておけばBake後の作業が楽になるかと思います。</p>
<p>例えば「app_controller.php」には次のコードを記載しています。</p>
<pre class="brush:php">function _setPageTile($string=NULL,$locale=true){
	$title=($locale)?__($string,true):$string;
	$this-&gt;set('title_for_layout',$title);
}
</pre>
<p>これはCakePHPの1.2系では次のコードをコントローラーの各アクションに記載することで<br />
ページタイトル用の変数「$title_for_layout」へ自動的にセットされていましたが<br />
CakePHP1.3系では無くなってしまったので、代用コードで利用しています。</p>
<pre class="brush:php">$this-&gt;pageTitle=’タイトル名’;
</pre>
<p>ちなみにCakePHP1.2系から1.3系にアップグレードした場合,<br />
「app_controller.php」に次のコードを記載すると<br />
コードの変更をすること無く利用できるかと思います。</p>
<pre class="brush:php">function afterFilter(){
	if(isset($this-&gt;pageTitle) &amp;&amp; !empty($this-&gt;pageTitle)){
		$this-&gt;set('title_for_layout',__($this-&gt;pageTitle,true));
	}
}
</pre>
<p>※各コントローラーで「afterFilter()」が存在し、「parrent::afterFilter()」が<br />
記述されていない場合は記述する必要があるので注意が必要です。
<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.widget-info.net%2F2010%2F11%2Fcakephp-bake-2%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.widget-info.net/2010/11/cakephp-bake-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.widget-info.net/2010/11/cakephp-bake-2/" />
	</item>
		<item>
		<title>CakePHP　Bake後の出力をカスタマイズ[その1]</title>
		<link>http://blog.widget-info.net/2010/11/cakephp-bake-1/</link>
		<comments>http://blog.widget-info.net/2010/11/cakephp-bake-1/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 15:50:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[bake]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.widget-info.net/?p=433</guid>
		<description><![CDATA[CakePHPのBake機能を利用していて毎回Bake後に出力された内容にコーディング規約等合わせて手を加える場合があるかと思います。]]></description>
			<content:encoded><![CDATA[<p>CakePHPのBake機能を利用していて毎回Bake後に出力された内容に<br />
コーディング規約等合わせて手を加える場合があるかと思います。</p>
<p>今回はBake時に出力されるCakePHPをカスタマイズする方法について記載していこうかと思います。<br />
※自分の備忘録でもあるので、ご存じの方も多いかと思います（笑<br />
<span id="more-433"></span><br />
まずはBake時に出力される「app」の内容はどこからきているのか。。。<br />
これは次のCakePHPディレクトリにあります。</p>
<pre>cake/console/templates
</pre>
<p>このディレクトリがBake時に出力される元のコード類になっています。<br />
例えば、このディレクトリにある「default」ディレクトリには<br />
モデルやコントローラーをBakeするときのテンプレートファイルや<br />
ビューを出力するときのテンプレートファイルがあります。</p>
<p>「default」ディレクトリの他に「skel」のディレクトリがあります。<br />
このディレクトリではBakeを実行時に最初に出力されるファイル類です。<br />
「app_controller.php」や「webroot」ディレクトリなどがあります。<br />
いつも使うコンポーネントやヘルパーをこの中に入れておいたり、<br />
「app_controller.php」や「app_model.php」に記述していることを<br />
あらかじめ記述しておくことで、Bake時に出力してくれるので、作業が楽になるかと思います。</p>
<p>とりあえず今回はここまで。。。
<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.widget-info.net%2F2010%2F11%2Fcakephp-bake-1%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.widget-info.net/2010/11/cakephp-bake-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.widget-info.net/2010/11/cakephp-bake-1/" />
	</item>
		<item>
		<title>CakePHP　Bakeの機能を変更してみる</title>
		<link>http://blog.widget-info.net/2009/10/cakephp_bake/</link>
		<comments>http://blog.widget-info.net/2009/10/cakephp_bake/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 22:29:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[bake]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Controller]]></category>

		<guid isPermaLink="false">http://blog.widget-info.net/?p=330</guid>
		<description><![CDATA[CakePHPにはBakeがあります。データベースを元に登録画面や一覧画面などを自動で出力してくれる機能です。]]></description>
			<content:encoded><![CDATA[<p>CakePHPにはBakeがあります。皆様利用されている方も多いかと思います。<br />
データベースを元に登録画面や一覧画面などを自動で出力してくれる機能です。<br />
拡張すれば独自の機能も作れますが、その話はどこかで。。。</p>
<p>今回は基本的にはCakePHPの本体を変更するのはあまりしたくはないのですが<br />
Bake時に出力される「Controller」が個人的にはそのままでは使いづらく<br />
開発の効率が若干無駄になる気がしたので変更してみました。<br />
<span id="more-330"></span><br />
変更したのは「cake/console/libs/tasks」にある「controller.php」の中を変更。<br />
変更したコードは「bakeActions()」内を次のように変更し、どのような機能にしたかというと<br />
コントローラー名を基本的に記述させないことと、セッションメッセージの出力を変更したことが主になります。</p>
<pre class="brush:php">
	function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
		$currentModelName = $modelImport = $this->_modelName($controllerName);
		if ($this->plugin) {
			$modelImport = $this->plugin . '.' . $modelImport;
		}
		if (!App::import('Model', $modelImport)) {
			$this->err(__('You must have a model for this class to build scaffold methods. Please try again.', true));
			exit;
		}
		$actions = null;
		$modelObj =&#038; new $currentModelName();
		$controllerPath = $this->_controllerPath($controllerName);
		$pluralName = $this->_pluralName($currentModelName);
		$singularName = Inflector::variable($currentModelName);
		$singularHumanName = Inflector::humanize($currentModelName);
		$pluralHumanName = Inflector::humanize($controllerName);
		$actions .= "\n";
		$actions .= "\tfunction {$admin}index() {\n";
		$actions .= "\t\t\$this->{\$this->modelClass}->recursive = 0;\n";
		$actions .= "\t\t\$this->set('{$pluralName}', \$this->paginate(\$this->modelClass));\n";
		$actions .= "\t}\n";
		$actions .= "\n";
		$actions .= "\tfunction {$admin}view(\$id = null) {\n";
		$actions .= "\t\tif (!\$id) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\$this->Session->setFlash(sprintf(__('Invalid %s.', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\$this->flash(sprintf(__('Invalid %s.', true)),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t}\n";
		$actions .= "\t\t\$this->set('" . $singularName . "', \$this->{\$this->modelClass}->read(null, \$id));\n";
		$actions .= "\t}\n";
		$actions .= "\n";

		/* ADD ACTION */
		$compact = array();
		$actions .= "\tfunction {$admin}add() {\n";
		$actions .= "\t\tif (!empty(\$this->data)) {\n";
		$actions .= "\t\t\t\$this->{\$this->modelClass}->create();\n";
		$actions .= "\t\t\tif (\$this->{\$this->modelClass}->save(\$this->data)) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\t\$this->Session->setFlash(sprintf(__('The %s has been saved', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\t\$this->flash(sprintf(__('The %s has been saved.', true),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t\t} else {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\t\$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true),__(\$this->modelClass,true)));\n";
		}
		$actions .= "\t\t\t}\n";
		$actions .= "\t\t}\n";
		foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
			if (!empty($associationName)) {
				$habtmModelName = $this->_modelName($associationName);
				$habtmSingularName = $this->_singularName($associationName);
				$habtmPluralName = $this->_pluralName($associationName);
				$actions .= "\t\t\${$habtmPluralName} = \$this->{\$this->modelClass}->{$habtmModelName}->find('list');\n";
				$compact[] = "'{$habtmPluralName}'";
			}
		}
		foreach ($modelObj->belongsTo as $associationName => $relation) {
			if (!empty($associationName)) {
				$belongsToModelName = $this->_modelName($associationName);
				$belongsToPluralName = $this->_pluralName($associationName);
				$actions .= "\t\t\${$belongsToPluralName} = \$this->{\$this->modelClass}->{$belongsToModelName}->find('list');\n";
				$compact[] = "'{$belongsToPluralName}'";
			}
		}
		if (!empty($compact)) {
			$actions .= "\t\t\$this->set(compact(" . join(', ', $compact) . "));\n";
		}
		$actions .= "\t}\n";
		$actions .= "\n";

		/* EDIT ACTION */
		$compact = array();
		$actions .= "\tfunction {$admin}edit(\$id = null) {\n";
		$actions .= "\t\tif (!\$id &#038;&#038; empty(\$this->data)) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\$this->Session->setFlash(sprintf(__('Invalid %s', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\$this->flash(sprintf(__('Invalid %s', true),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t}\n";
		$actions .= "\t\tif (!empty(\$this->data)) {\n";
		$actions .= "\t\t\tif (\$this->{\$this->modelClass}->save(\$this->data)) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\t\$this->Session->setFlash(sprintf(__('The %s has been saved', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\t\$this->flash(sprintf(__('The %s has been saved.', true),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t\t} else {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\t\$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true),__(\$this->modelClass,true)));\n";
		}
		$actions .= "\t\t\t}\n";
		$actions .= "\t\t}\n";
		$actions .= "\t\tif (empty(\$this->data)) {\n";
		$actions .= "\t\t\t\$this->data = \$this->{\$this->modelClass}->read(null, \$id);\n";
		$actions .= "\t\t}\n";

		foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
			if (!empty($associationName)) {
				$habtmModelName = $this->_modelName($associationName);
				$habtmSingularName = $this->_singularName($associationName);
				$habtmPluralName = $this->_pluralName($associationName);
				$actions .= "\t\t\${$habtmPluralName} = \$this->{\$this->modelClass}->{$habtmModelName}->find('list');\n";
				$compact[] = "'{$habtmPluralName}'";
			}
		}
		foreach ($modelObj->belongsTo as $associationName => $relation) {
			if (!empty($associationName)) {
				$belongsToModelName = $this->_modelName($associationName);
				$belongsToPluralName = $this->_pluralName($associationName);
				$actions .= "\t\t\${$belongsToPluralName} = \$this->{\$this->modelClass}->{$belongsToModelName}->find('list');\n";
				$compact[] = "'{$belongsToPluralName}'";
			}
		}
		if (!empty($compact)) {
			$actions .= "\t\t\$this->set(compact(" . join(',', $compact) . "));\n";
		}
		$actions .= "\t}\n";
		$actions .= "\n";
		$actions .= "\tfunction {$admin}delete(\$id = null) {\n";
		$actions .= "\t\tif (!\$id) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\$this->Session->setFlash(sprintf(__('Invalid id for %s', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\$this->flash(sprintf(__('Invalid %s', true),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t}\n";
		$actions .= "\t\tif (\$this->{\$this->modelClass}->del(\$id)) {\n";
		if ($wannaUseSession) {
			$actions .= "\t\t\t\$this->Session->setFlash(sprintf(__('%s deleted', true),__(\$this->modelClass,true)));\n";
			$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
		} else {
			$actions .= "\t\t\t\$this->flash(sprintf(__('%s deleted', true),__(\$this->modelClass,true)), array('action'=>'index'));\n";
		}
		$actions .= "\t\t}\n";
		$actions .= "\t}\n";
		$actions .= "\n";
		return $actions;
	}
</pre>
<p>ほぼ変えてしまったので、まるまる記載しました。<br />
かなり長くはなります（笑
<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.widget-info.net%2F2009%2F10%2Fcakephp_bake%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.widget-info.net/2009/10/cakephp_bake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.widget-info.net/2009/10/cakephp_bake/" />
	</item>
	</channel>
</rss>

