I’m writing this post for two reasons:

  1. So others can learn from my mistakes
  2. So I have this documented somewhere other than my laptop

This post is almost completely a copy and paste job of the notes I created while I was building my last project using Symfony 2.7.

SYMFONY_ENV

I’ve found that it’s important to specify which SYMFONY_ENV you’re in when you’re running composer. I ran into a series of problems with I first went to install my dependencies on my production box. The fix has been specifying the SYMFONY_ENV:

SYMFONY_ENV=prod php composer.phar install --no-dev --optimize-autoloader

Doctrine

I’m really loving Doctrine. I love how you can define the columns and relationships inside the models and how it easily creates all the related structures. It’s so much nicer than the horrible system I have of using MySQL Workbench to create schema files based on the differences.

Generate getters and setters

To have Doctrine generate the getters and setters you can run:

php app/console doctrine:generate:entities Njcc/WebsiteBundle/Entity

Migrations Bundle

I’ve found the Doctrine Migrations Bundle to be very helpful. The Doctrine bundle doesn’t provide a way to add default data (information you might need for a select) so I started using this early in the project. I’m already a big fan of database versioning so it didn’t take much to get me onboard with this.

To create a diff file:

php app/console doctrine:migrations:diff

To apply a diff file

php app/console doctrine:migrations:migrate -n

To create a blank migrations file (to create default data):

php app/console doctrine:migrations:generate

Random Helpful Commands:

I’ve found these to be helpful more than once.

Debug the Router:

php app/console router:debug --env=prod

Clear the Cache

php app/console cache:clear --env=prod

Dump the assetic assets

php app/console assetic:dump --env=prod

Twig Permissions

In order to check if the current user has a role:

{% if is_granted('ROLE_ADMIN') %}

Twig Dump Function

I found this to be useful in figuring out what it was I was working with. It’s like php’s var_dump function.

{{ dump(tableData) }}

Twig For Loop Variables

Twig provides several helpful variables for determining where you’re at in a loop. I’m not going to copy and paste it into this post because it can be found here:

http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

Getting The Current User In a Controller

I don’t know why but this seemed overly complicated to me. I also ran into a lot of problems understanding the difference between IS_AUTHENTICATED_REMEMBERED and IS_AUTHENTICATED_FULLY.

$user = $this->get('security.context')->getToken()->getUser();

Fill a Select With Values From a Database Table

I was shocked at how easy this was:

<?php
    ->add('family', 'entity', array(
        'class' => 'NjccWebsiteBundle:Family',
        'property' => 'name',
    ))

The important things to note are that ‘family’ is the name of the element, ‘entity’ is the type, ‘class’ indicates the entity, and ‘property’ indicates what’s the option inside the select.

Dynamical Adding Elements in a Form

Sadly this was complicated. To the point where I don’t every want to have to reiterate this and will just give you a link to more information.

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-underlying-data

FOS User Bundle

I think the FOS User Bundle saved me a bunch of time because I didn’t have to write my own login/registration system. The only thing I didn’t find easy was learning how to create a user account when you’re first setting up your site (so you have an admin account). It’s done with the following:

php app/console fos:user:create adminuser --super-admin

More information can be found at http://symfony.com/doc/current/bundles/FOSUserBundle/command_line_tools.html

Performance Problems Running in a VM

I’m running my development environment in a VM and I found the site to be sluggish to the point where I actually questioned my use of Symfony. To fix this problem I had to add the following to my AppKernel to improve the performance. These lines move the cache and the logs to a local directory.

<?php
class AppKernel extends Kernel
{
    public function getCacheDir()
    {
        if ($this->environment == 'dev') {
            return '/tmp/cache/' . $this->environment;
        }
        return parent::getCacheDir();
    }

    public function getLogDir()
    {
        if ($this->environment == 'dev') {
            return '/tmp/logs';
        }
        return parent::getLogDir();
    }
}

Conclusion

Overall, I’m very happy I went with Symfony for this project even if there was a huge uphill climb to get the project up and running. I’m starting to work on another project and any future projects will be using Symfony.