Screen Shot 2014-11-25 at 8.18.03 AM I’m still trying to work through the Getting Started tutorial and I’m finding that it is apparently out of date. Or at least it’s showing the wrong code with the wrong version.

The examples in the tutorial do not work for me. I’m using version ‘2.57 app/dev/debug’. I found the version using the following on my command line:

$ cd /path-to-myproject/myproject
$ app/console --version

I did finally get the ‘Hello’ formatting example to work by doing some Google searches and re-reading the tutorial. I almost gave up and quit the tutorial. Since there were errors early on in the documentation, I don’t trust the documentation and it’s hard to decide what’s an error on my part and what’s an error in the documentation. I’m glad I kept trying and found that some of the problems were my own errors.

Many are not, but digging deeper and trying to figure out what I did wrong has helped me to find the correct way to do it in Symfony. I’ve learned a lot about Symfony as a result.

Here are some of the things I’ve learned:

  • Make sure to change the Annotations (which look like comments) above the actions in the controller. These have important routing information that lets Symfony know where to find the templates.
  • Don’t forget to add in the proper use statements at the top of the controller or you will get messages such as this:
    Attempted to load class "Response" from namespace "Acme\DemoBundle\Controller"
  • The Render should look like this instead of what’s posted on the tutorial:
    return $this->render('AcmeDemoBundle:Demo:hello.'.$_format.'.twig', array('name' => $name));
  • In the ‘Redirecting and Forwarding’ section, the method redirectToRoute is not available in version 2.5.7.

    According to the documentation, that method is introduced in version 2.6 which hasn’t really been released yet, only beta versions. When I try to switch to version 2.6 in the Getting Started tutorial, it goes to version 3.0, but shows the same documentation as it does for 2.5. Using this new method in my version 2.5.7 gives me the following error:

    Attempted to call method "redirectToRoute" on class "AcmeDemoBundleControllerDemoController" in ...

    I got it to work using this instead, which is apparently the old way to do it:

    return $this->redirect($this->generateUrl('hello', array('name' => 'Fabien')));
  • I got the ‘forward’ to work using this:
    return $this->forward('AcmeDemoBundle:Demo:hello', array('name'  => 'Fabien', '_format' => 'html'));
  • I couldn’t get the {{ app.request.parameter('page') }} to work in the twig template, but I did get it to work like this: {{ app.request.get('page') }}.
  • The code for setting and getting cookies worked really well, but I had a little trouble with the flash messages. After trial and error I got it to work.

    The addFlash method didn’t work for me. After a little searching I found that this works:

    $session->getFlashBag()->add('notice', 'Congratulations, your action succeeded!');

    The Twig code that’s used in the tutorial for displaying the flash messages didn’t work either. It was showing up whether I had the code in the template or not. I finally realized that it’s because of the

    {% extends "AcmeDemoBundle::layout.html.twig" %}

    at the top of the page which prints out flash messages. I took that out so I could try out the tutorial code, but the code from the tutorial gave me an exception:

    Notice: Array to string conversion in

    I found where adding [0] to the end of the line works:

    {{ app.session.flashbag.get('notice')[0] }}

    This works as long as a flash message has been set, but gives this error when one is not set:

    Key "0" does not exist as the array is empty in

    I finally got it to work using this code:

    {% if app.session.flashbag.has('notice') %}
        <div>{{ app.session.flashbag.get('notice')[0] }}</div>
    {% endif %}

Even though some of the code isn’t working as written, I’m still learning a lot about Symfony by trying out the code, then debugging and searching to see why it’s not working. It’s been a really helpful exercise in learning how to use Symfony again.

Now, I’m anxious to move on to the Architecture Tutorial!

2 thoughts on “Symfony Getting Started Tutorial – The Controller”

Leave a Comment

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