bake.php - Easy baking for lazy folks

When I first tried Ruby on Rails I was literally amazed by the generator script. Yes, I was young and inexperienced then (six/seven months ago), but you must admit that getting a controller, a model, all the basic views generated automatically by

rails script/generator scaffold Posts

is not a bad thing. Especially if the same script allows you to create model, views and controller separately and other things. Symfony and PHP on Trax already tried to port this functionalities, with mixed results. What about Cake? Oh well, yes, we do have something like that… something rather different, but still something: the bake.php script.
This cute little thing is located in the cake/scripts/ folder and can be used – hear, hear – from command line. You can run Ruby and Perl scripts, so yes, you can actually run PHP from command line, although it's not its primary purpose.

Cool then, let's open a *nix shell, Windows command prompt, etc. etc., go into the cake/scripts/ folder and run:

php bake.php

Assuming that the php executable is in your PATH environment variable – if not, either you add it or you'll have to type something like:

D:SERVERphpphp.exe bake.php

depending on where your php executable is. You'll be be greeted by a “CAKEPHP BAKE” text, and then you'll be asked a few questions. One thing to realize before proceeding any further: bake.php is not a generator, not in the traditional “Rails” sense, anyway. It's rather a handy but more verbose dialogue-based configuration script – which will also generate something eventually if you provide all the necessary details.
A different approach, which may be good or bad according to your taste: personally I think we should also have something faster to use, like a Rails generator, and I opened a ticket about it, but let's see what bake.php can do, for now.

The answer is… nearly anything. It annoying enough to please, but if you follow its directions it can do a prettu decent job in the end, it's far from being sentient, but let's say it's smart enough for a script. First of all if you try it out on a fresh Cake install it will notice that you haven't configured your database yet, so it will ask for a hostname, username, password, database name etc. etc. and generate your app/config/database.php for you, not a bad start.

Once that's done – and it won't go on unless you configure a (MySQL only?) database – you can proceed with the rest. You can start creating either a controller, model or view; I tried a Posts controller, for example. The script then asks quite a few questions:

  • The controller's name
  • Whether it will use other models besides posts
  • Whether you want to include any helper
  • Whether you want to include any component
  • Whether you want to generate the base CRUD methods

Then finally it generates the damn thing. The result is good enough:


<?php
class PostsController extends AppController
{
	//var $scaffold;
	var $name       = 'Posts';</p>
<p>function index()<br />
	{<br />
		$this&#8594;set(&#8216;data', $this&#8594;Post&#8594;findAll());<br />
	}</p>
<p>function add()<br />
	{<br />
		if(empty($this&#8594;params[&#8216;data']))<br />
		{<br />
			$this&#8594;render();<br />
		}<br />
		else<br />
		{<br />
			if($this&#8594;Post&#8594;save($this&#8594;params[&#8216;data']))<br />
			{<br />
				$this&#8594;flash(&#8216;Post saved.', &#8216;/posts/index');<br />
			}<br />
			else<br />
			{<br />
				$this&#8594;render();<br />
			}<br />
		}<br />
	}</p>
<p>function edit($id)<br />
	{<br />
		if(empty($this&#8594;params[&#8216;data']))<br />
		{<br />
			$this&#8594;set(&#8216;data', $this&#8594;Post&#8594;find(&#8216;Post.id = ' . $id));<br />
		}<br />
		else<br />
		{<br />
			if($this&#8594;Post&#8594;save($this&#8594;params['data']))<br />
			{<br />
				$this&#8594;flash(&#8216;Post saved.', &#8216;/posts/index');<br />
			}<br />
			else<br />
			{<br />
				$this&#8594;set(&#8216;data', $this&#8594;params[&#8216;data']);<br />
				$this&#8594;validateErrors($this&#8594;Post);<br />
				$this&#8594;render();<br />
			}<br />
		}<br />
	}</p>
<p>function view($id)<br />
	{<br />
		$this&#8594;set(&#8216;data', $this&#8594;Post&#8594;find('Post.id = ' . $id));<br />
	}</p>
<p>function delete($id)<br />
	{<br />
		$this&#8594;Post&#8594;del($id);<br />
		$this&#8594;redirect(&#8216;/posts/index');<br />
	}</p>
<p>function postList()<br />
	{<br />
		$vars = $this&#8594;Post&#8594;findAll();<br />
		foreach($vars as $var)<br />
		{<br />
			$list[$var[&#8216;Post'][&#8216;id']] = $var[&#8216;Post'][&#8216;name'];<br />
		}</p>
<p>return $list;<br />
	}<br />
}<br />
?><br />

It's more or less the same with models and views: it will still ask a lot of questions and in the end generate the thing.
This behaviour is more advanced than a standard generator, you can include helpers and components already, if you want, but do you really want that? For models it even asks if you want to include particular associations and validation rules! Personally, I'd rather a generator script which generates something immediately and accepts maybe some parameters to further customization, like:

php bake.php scaffold Posts
php bake.php controller Posts
php bake.php model Posts
php bake.php model Posts
php bake.php controller Posts helper +Html -Time,Javascript
php bake.php model Posts assoc +hasMany comments,tags

Bah… just some random thoughts. How about custom-made generators (Rails-inspired)?