Killer Crud: Symfony Forms Handle the Dirty Work

Bundle Creation

To start this tutorial we'll need a bundle, which is like a drupal module, or a wordpress plugin... or a Magento extension. Many names for the same thing with is a package of code that interacts with the core components to provide routes and data etc.

Symfony's console has a bunch of commands to help stub things out and auto generate, to get the boring stuff out of the way! To create a bundle run the generate:bundle command and answer the prompts.

app/console generate:bundle \ --namespace=Titan/KillerCrudBundle \ --dir=/var/www/killercrud/src \ --format=yml \ --bundle-name=TitanKillerCrudBundle \ --structure=yes You are going to generate a "Titan\KillerCrudBundle\TitanKillerCrudBundle" bundle in "/private/var/www/killercrud/src/" using the "yml" format. Do you confirm generation [yes]?

Model Creation

Now that we have a bundle we have a place to build our CRUD application. So we'll need a model but what sort of business logic does the killer crud bundle deal with?

Wild Wild Web Contracts

The backstory is we're the new posse of coding cowboys in town and we're looking to drum up some business. To do this we'll need to create some contracts to ensure our business doesn't get hustled by cattle rustlers.

The properties we'll need in this model are the company's name, their website url, the amount of money the contract is worth and a description of the work to be done. This is obviously over simplified for the example, and heck we're cowboys!

We can jumpstart model creation with the doctrine:generate:entity command. You can set this up with flags too but the interactive prompts are really nice...

app/console doctrine:generate:entity --entity=TitanKillerCrudBundle:Contract ... New field name (press <return> to stop adding fields): company Field type [string]: Field length [255]: New field name (press <return> to stop adding fields): address Field type [string]: Field length [255]: New field name (press <return> to stop adding fields): amount Field type [string]: integer New field name (press <return> to stop adding fields): description Field type [string]: text ... Do you confirm generation [yes]?

This will generate a directory inside the bundle called Entity, which will have two files. The model, Contract and a doctrine repository to help service the model called ContractRepository. The repository is used to provide convienence to commonly used fetch methods. The repository has direct access to the DQL layer and is designed to serve up only collections of Contract objects.

Crud Creation

The CRUD generation method uses the entity/model definition to determine what sort of form it will need to build. The controller functions and route settings line up nicely to a simple yet effective REST architecture.

app/console doctrine:generate:crud \ --entity=TitanKillerCrudBundle:Contract \ --route-prefix=contract \ --with-write=yes \ --format=annotation

The CRUD generation creates a ContractController and a few views. By choosing annotation routing, it cuts down on the extra file which would be under Resources/config/routing/contract.yml.

Now we can navigate around the basic CRUD application that we have built using only Symfony 2 commands. You'll quickly notice that the application is on a stark white background with very mismatched controls that are not styled in any manner. This is bad and good, for our snazzy spree of getting things done with stubs, it's bad. By Symfony being front end agnostic it frees us to choose any framework or library we're interested in implementing.

Chapter 2: Styling with Bootstrap

In the next article we'll use Twitter Bootstrap to apply some basic styles and design. We'll learn how to customize form themes and apply custom rendering techniques to gain absolute control over how each element is placed and styled.