contents/articles/26.html
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 |
----- title: bake.php - Easy baking for lazy folks content-type: article timestamp: 1146922980 tags: cakephp|frameworks ----- <p>When I first tried Ruby on Rails I was literally amazed by the <em>generator</em> 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</p> <p><code>rails script/generator scaffold Posts</code></p> <p>is not a bad thing. Especially if the same script allows you to create model, views and controller separately and other things. <a href="http://www.symfony-project.com/">Symfony</a> and <span class="caps">PHP</span> 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 <code>bake.php</code> script.<br /> This cute little thing is located in the <code>cake/scripts/</code> folder and can be used – hear, hear – from command line. You can run Ruby and Perl scripts, so yes, you can actually run <span class="caps">PHP</span> from command line, although it’s not its primary purpose.</p> <p><img src="/img/pictures/bake.jpg" alt="" /></p> <p>Cool then, let’s open a *nix shell, Windows command prompt, etc. etc., go into the <code>cake/scripts/</code> folder and run:</p> <p><code>php bake.php</code></p> <p>Assuming that the php executable is in your <em><span class="caps">PATH</span></em> environment variable – if not, either you add it or you’ll have to type something like:</p> <p><code>D:SERVERphpphp.exe bake.php</code></p> <p>depending on where your php executable is. You’ll be be greeted by a “<span class="caps">CAKEPHP</span> <span class="caps">BAKE</span>” text, and then you’ll be asked a few questions. One thing to realize before proceeding any further: bake.php is <em>not</em> 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 <em>something</em> eventually if you provide all the necessary details.<br /> 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 <a href="https://trac.cakephp.org/ticket/768">ticket</a> about it, but let’s see what bake.php can do, for now.</p> <p>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 <code>app/config/database.php</code> for you, not a bad start.</p> <p>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 <code>Posts</code> controller, for example. The script then asks quite a few questions:</p> <ul> <li>The controller’s name</li> <li>Whether it will use other models besides posts</li> <li>Whether you want to include any helper</li> <li>Whether you want to include any component</li> <li>Whether you want to generate the base <span class="caps">CRUD</span> methods</li> </ul> <p>Then finally it generates the damn thing. The result is good enough:</p> <p><small><br /> <pre><code> <?php class PostsController extends AppController { //var $scaffold; var $name = 'Posts';</p> <p>function index()<br /> {<br /> $this→set(‘data’, $this→Post→findAll());<br /> }</p> <p>function add()<br /> {<br /> if(empty($this→params[‘data’]))<br /> {<br /> $this→render();<br /> }<br /> else<br /> {<br /> if($this→Post→save($this→params[‘data’]))<br /> {<br /> $this→flash(‘Post saved.’, ‘/posts/index’);<br /> }<br /> else<br /> {<br /> $this→render();<br /> }<br /> }<br /> }</p> <p>function edit($id)<br /> {<br /> if(empty($this→params[‘data’]))<br /> {<br /> $this→set(‘data’, $this→Post→find(‘Post.id = ’ . $id));<br /> }<br /> else<br /> {<br /> if($this→Post→save($this→params[’data’]))<br /> {<br /> $this→flash(‘Post saved.’, ‘/posts/index’);<br /> }<br /> else<br /> {<br /> $this→set(‘data’, $this→params[‘data’]);<br /> $this→validateErrors($this→Post);<br /> $this→render();<br /> }<br /> }<br /> }</p> <p>function view($id)<br /> {<br /> $this→set(‘data’, $this→Post→find(’Post.id = ’ . $id));<br /> }</p> <p>function delete($id)<br /> {<br /> $this→Post→del($id);<br /> $this→redirect(‘/posts/index’);<br /> }</p> <p>function postList()<br /> {<br /> $vars = $this→Post→findAll();<br /> foreach($vars as $var)<br /> {<br /> $list[$var[‘Post’][‘id’]] = $var[‘Post’][‘name’];<br /> }</p> <p>return $list;<br /> }<br /> }<br /> ?><br /> </code></pre><br /> </small></p> <p>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. <br /> This behaviour is more advanced than a standard generator, you can include helpers and components already, if you want, but do you <em>really</em> 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 <em>immediately</em> and accepts maybe some parameters to further customization, like:</p> <p><code>php bake.php scaffold Posts</code><br /> <code>php bake.php controller Posts</code><br /> <code>php bake.php model Posts</code><br /> <code>php bake.php model Posts</code><br /> <code>php bake.php controller Posts helper +Html -Time,Javascript</code><br /> <code>php bake.php model Posts assoc +hasMany comments,tags</code></p> <p>Bah… just some random thoughts. How about custom-made generators (<a href="http://wiki.rubyonrails.org/rails/pages/AvailableGenerators">Rails-inspired</a>)?</p> |