Code Faster Symfony2s Interactive Generators
Jul 3rd, 2011When building a Symfony2 application, there are some tasks that you always do. In the lifetime of every project, you will create around 2 bundles, 5 and 10 entities, and for almost all entities, you will implement a CRUD (Create, Read, Update, Delete) interface with controllers and templates to deal with them.
Symonfy2’s interactive generators save you a lot of time. Currently, there are 3 of them:
generate:bundle
generates a new bundlegenerate:doctrine:entity
generates a new entitygenerate:doctrine:crud
generates a CRUD based on an entity
Before starting, get excited by watching the screencast made by the Symfony2 team.
New Bundle
To create a new bundle, run the command:
app/console generate:bundle
It will launch an assistant that will ask you these questions: Bundle namespace, Bundle name, Target directory, Configuration format (yml, xml, php, or annotation). And you have a running bundle, you don’t have to bother about anything else! The script took care of adding the bundle to the autoloader, subscribing it in the kernel and adding routing rules. It has already a default controller with a basic function that you can start to play with:
And a template for this controller:
Hello !
New Entity
Instead of creating all your entities by creating and editing the PHP
files in MyBundle/Entity
, just run the command:
app/console generate:doctrine:entity
Follow the wizard and declare the name of your entity and the fields you
want. The script will generate all the code for you. Here is an example
of the code generated with only one field name
:
See how much time you just saved ? It can even generate an empty
repository for this entity if you say yes
during when you are asked
the question:
Entity CRUD generator
But the real time saver is the CRUD generator. You run the command:
app/console generate:doctrine:crud
Answer the question (what bundle, write actions or not, route prefix,
etc.) and it will generate a new controller with all the method to deal
with this entity and the corresponding templates. Here is for example
the update
action for my entity that the script just generated:
and the template:
<h1>Post edit</h1>
<form action="{{ path('post_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
{{ form_widget(edit_form) }}
<p>
<button type="submit">Edit</button>
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('post') }}">
Back to the list
</a>
</li>
<li>
<form action="{{ path('post_delete', { 'id': entity.id }) }}" method="post">
{{ form_widget(delete_form) }}
<button type="submit">Delete</button>
</form>
</li>
</ul>
Now you just saved one day of work, you can take your week-end on thursday night! Further reading:
- Blog post announcing the interactive generators
- Repository on github of the bundle powering those generator (fork it!)
And more commands to come!