Symfony Cheatsheet

With the newest version of Symfony coming out soon, you may need to change “app/console” commands to “bin/console” below. Be sure you are in the Symfony folder in your website in the command line and you are the proper web user. Don’t run these commands as “root” or you will break files.

If you need the latest compatible Symfony Composer dependency list, click here.

To clear the cache for dev environment, omit the “–env=prod” option, which clears the production environment cache.

Make a new bundle skeleton. Navigate to your Symfony project first. “Acme” is always the demo, so replace with your own bundle namespace.

Generate CRUD forms, (Create, Read, Update & Destroy) automatically. Leaving out options will bring up a list of questions before creating the CRUD forms:

Get current route. What’s a route?!

You can find the class used as the provider-service using:

List all registered routes:

Dump assets to production environment:

Please note above: Just checking if you are paying attention! The new Symfony folder structure is coming and the console will be in the “/bin” folder in the future.

To generate entities from an existing database in Symfony, you first need Doctrine to introspect the database, then generate the entities. Be careful, your database relationships will probably need to be tweaked, getting your Doctrine table relationships mapped correctly is very important and might be a bit time consuming until you get a handle on it!

Note above: I despise annotations in my code, I ALWAYS use YML files.

Get more information about current Doctrine entity mappings:

Redirect to a URL from a controller without a route:

Accessing unmapped form fields from controller:

Since we’re on the FOS User Bundle, here’s a way to add a new user and change that user’s password. Shell command through Putty on the command line. Change “admin” user’s password to “BeLchamber”:

Symfony Session variables, “flash bag”


Symfony Forms

The best, easy-to-follow example of how to persist multiple related entities upon form submission with Doctrine. This is a sample controller action. Thanks to users from Stack Overflow:

Know if page was reached via POST:

Get current URL of script:

If you like to let Symfony create CRUD for your database tables, you might freeze up the computer if you access the “index” action since this will list all records of the table by default to the browser all at once. Here’s a way to set limits from repository queries:

Troubleshooting form submissions. This method is being replaced, but still works for Symfony 2.5!

Symfony Doctrine DBAL & ORM

Get Doctrine var_dump debug output in your controller:

Other useful snippets:

To install Braincrafted Bootstrap for Symfony

CDN to Bootstrap.

Versions will probably have changed, but these are lts stable versions:

Quick shell commands that I seem to forget for some reason. Find a “directory” named “node_modules” for instance. “-type d” means “Directory”, change to “-type f” for file instead:

– Aaron Belchamber
Aaron Belchamber’s Marketing & Video Services Website

3 thoughts on “Symfony Cheatsheet

Leave a Reply

Your email address will not be published. Required fields are marked *