Implementing Disqus comments in Symfony 2 and 3


Disqus is a networked community platform for your website. To learn more about Disqus, read What is Disqus or visit the the Disqus website.

Whether websites use Disqus plugins or just manually embed the script, the system is loaded onto the page in generally the same way.

When a user visits a webpage that includes Disqus (for example, a blog post), the page makes a request to Disqus. Disqus uses the information defined on the page, called configuration variables, to locate the correct thread. Disqus will look up the associated thread and, if found, embed the correct threads with all the right comments onto the page. If an associated thread was not found, Disqus will create a new page with the data provided (again, in the configuration variables), and environment metadata such as page URL, page title, and current datetime.

An awesome tool that will help you to solve the problem of create tables in your database project handling the user-comments-blog relations and you may want to implement as soon as possible.

Register your website in Disqus

We suppose that you already have a Disqus account, now go the the register site area and fill the formular. After the registration of your website, go to the admin panel of disqus.

Add code

Choose a website and choose a platform to install, in this case select the Universal Code.

The Disqus website will show you the HTML code that you need to append in your website, i.e :

Note: use twig to easily retrieve the canonical url of the page. Remember that the following script needs to be generated by the Disqus website, you only need to change the values of the disqus_config variable.

<div id="disqus_thread"></div>
<script>
    /**
     *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
     *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
     */
    
    var disqus_config = function () {
// Canonical URL (full url generated by twig) this.page.url = '{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}'; // Replace PAGE_URL with your page's canonical URL variable
// Here you need to add the identifier of this page, for example if is a page of a blog, the identifier would be : /blog/april/how-to-breath/12 this.page.identifier = '/blog/april/how-to-breath/12'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = '//mywebsite.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })(); </script> <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
  • identifier : needs to be the relative url path of the page where the comments needs to be loaded, identified as an id in the disqus database.
  • url : the canonical path to the website where the comments will be loaded (http://mywebsite/blog/april/how-to-breath/12).
Become a more social person