all repos — h3rald @ e02ff3e430e9640525c851204d425dbd2f79bcd9

The sources of https://h3rald.com

content/articles/26.textile

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
----- 
permalink: "26"
filters_pre: 
- redcloth
title: bake.php - Easy baking for lazy folks
date: 2006-05-06 15:43:00 +02:00
tags: 
- cakephp
- frameworks
type: article
-----
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":http://www.symfony-project.com/ 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.

!http://base--/img/pictures/bake.jpg! 

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":https://trac.cakephp.org/ticket/768 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:

<small>
<pre><code>
<?php
class PostsController extends AppController
{
	//var $scaffold;
	var $name       = 'Posts';

	function index()
	{
		$this->set('data', $this->Post->findAll());
	}

	function add()
	{
		if(empty($this->params['data']))
		{
			$this->render();
		}
		else
		{
			if($this->Post->save($this->params['data']))
			{
				$this->flash('Post saved.', '/posts/index');
			}
			else
			{
				$this->render();
			}
		}
	}

	function edit($id)
	{
		if(empty($this->params['data']))
		{
			$this->set('data', $this->Post->find('Post.id = ' . $id));
		}
		else
		{
			if($this->Post->save($this->params['data']))
			{
				$this->flash('Post saved.', '/posts/index');
			}
			else
			{
				$this->set('data', $this->params['data']);
				$this->validateErrors($this->Post);
				$this->render();
			}
		}
	}

	function view($id)
	{
		$this->set('data', $this->Post->find('Post.id = ' . $id));
	}

	function delete($id)
	{
		$this->Post->del($id);
		$this->redirect('/posts/index');
	}

	function postList()
	{
		$vars = $this->Post->findAll();
		foreach($vars as $var)
		{
			$list[$var['Post']['id']] = $var['Post']['name'];
		}

		return $list;
	}
}
?>
</code></pre>
</small>
  
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":http://wiki.rubyonrails.org/rails/pages/AvailableGenerators)?