Facebook Like and Comments in Symfony2

Getting the full current URL without the GET parameters might be tricky.

The problem

If you followed the instruction to get the Facebook loke button or the Facebook comments plugin working on your website, you must have something like:

<div class="fb-like" data-href="http://my-website.com/my-page"></div>

For Facebook comments, you will have fb-comments instead of fb-like, but it's the same problem. You generate dynamically such piece of HTML with Twig in order to have:

<div class="fb-like" data-href="{{ app.request.uri }}">

But you may want to avoid GET parameters (for instance Google analytics parameters) like these ones:

http://my-website.com/my-page?utm_source=email&utm_campaign=blahblah

As comments and likes counts are for one unique URL, you will end up scattering your like count and comments accross all the URLs displaying the same page.

The fix

Here is the trick to get the current URL without the GET parameters, enjoy!

<div class="fb-comments" data-href="{{ app.request.getUriForPath(app.request.pathInfo) }}">
Quentin Pleplé
November 2011