phpStorm live templates (code snippets)

phpStorm has a wonderful feature called Live Templates that lets you quickly and easily define code templates (what I usually refer to as a snippet) for code you frequently have to write. For example:

  1. Foreach loops on a standard array of entities ($entities)
  2. Symfony isGranted checks via security.context
  3. Getters or setters that aren’t for standard properties
  4. Try/Catch blocks

And the list goes on..

Fortunately in phpStorm we can make this incredibly quick and easy:


Open phpStorm, and select phpStorm > Preferences from the menu. Within preferences, select Live Templates about 1/3 from the bottom.


Next, we need to add the new template (snippet) to the selected context. Select the language/environment you want to add to. Now click the “+” and then “Live Template” to create a new live template for the selected section.


Now define your template. Select the languages it applies to, and give it an abbreviation and a description. The abbreviation will also map the keystrokes you use later to access this template.


The easy part is writing the snippet itself. This example is an Entity class with a standard $id property and getter.


When you’ve saved from the previous step, all that’s left is to access the template in your files. In this example, I’ve selected PHP so all phpStorm looks for is the <?php tag to define the file as part of the PHP context. Once done, you simple click CMD+J and begin typing the abbreviation you used earlier. Select the template when you’ve found it, and voila.


A standard .gitignore for Symfony applications

I use phpStorm for most of my development unless I plan to get in and out very quickly, at which point I use SublimeText. phpStorm has a very useful tool to auto-generate a .gitignore file, but it doesn’t quite cover all the bases for my development environments (usually including Vagrant).

In either case, it’s always very useful to have a properly defined .gitignore so when you go to commit you’re not dealing with cleaning up files you didn’t want published, or silly environment files, docs, etc.


Yet Another LEMP Vagrant DEV Machine

This is my latest LEMP dev build. It’s carefully crafted after days of tweaking to include the following:

  1. Ubuntu 14.04 LTS
  2. NGINX 1.8.0
  3. PHP 5.6 (latest, currently 5.6.9)
  4. Ruby 2.2.2 (via RVM)
    1. Compass
    2. Sass
    3. Bundler
    4. CYAML
  5. curl
  6. openssl
  7. xdebug
  8. Composer

You can find it here, be sure to update the variables in the bash script to your specific parameters.