Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Knp Disqus Bundle Laravel Package

knplabs/knp-disqus-bundle

Symfony bundle for adding Disqus comments to your application. It handles Disqus configuration and provides Twig helpers to render comment threads, so you can embed community discussions with minimal boilerplate. Ideal for Symfony sites already using Disqus.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install and register. composer require knplabs/knp-disqus-bundle (the Disqus PHP client is pulled in transitively), then add Knp\Bundle\DisqusBundle\KnpDisqusBundle to config/bundles.php. As of v4.1.0 the package's composer.json finally allows PHP 8, so a plain install works on a modern PHP runtime — but note the release was a constraint bump only (no feature or bug-fix work), and the Symfony constraint was not widened. Run composer why-not knplabs/knp-disqus-bundle before committing if you're on Symfony 6+, and expect to tune or override if it complains.

  2. Get credentials. Create an API application at disqus.com/api/applications to obtain a public API key (the secret is only needed for OAuth; you do not need it for read-only comment fetching). Note your forum shortname (the *.disqus.com subdomain).

  3. Configure.

    # config/packages/knp_disqus.yaml
    knp_disqus:
        api_key: '%env(DISQUS_API_KEY)%'
        forum: 'my_blog'          # default forum shortname
        # cache: cache.app       # optional cache pool/service
        # forums: { ... }        # named forums if you run more than one
    

    Put DISQUS_API_KEY in .env.local / your secret store.

  4. First use case. Render a blog post's comment thread server-side so crawlers see real markup instead of the empty <div> that the Disqus JavaScript embed leaves behind:

    {{ knp_disqus_render('my_blog', 'post-' ~ post.id, {'limit': 25, 'order': 'asc'}) }}
    
  5. Where to look first. The bundle's DependencyInjection/Configuration.php (every option and its default), its Twig extension (the exact function signature and supported options), and Resources/views/ (the default templates you'll override). The README is short and mostly mirrors these.

Implementation Patterns

Use a stable thread identifier, not the URL. Disqus keys threads by ident and link; if you pass the URL, changing your route/slug orphans the thread. Always pass post-{id} (or another immutable key) as the identifier and let Disqus store the link separately.

Two rendering paths, pick per page:

  • Twig helper — fastest path. knp_disqus_render(forum, thread, options) fetches and renders the default comment template. Override the template in your app (standard bundle override path, e.g. templates/bundles/KnpDisqusBundle/...) to match your design.
  • Service in a controller/service — inject the bundle's Disqus service (the API wrapper, typically aliased to the bundle's main service id) and call ->fetch($forum, $thread, ['limit' => ..., 'order' => ...]). You get back a thread object with comments you can iterate, which is what you want if you need extra data on the page.

Drive more than comments from the same fetch. Build JSON-LD (DiscussionForumPosting / Comment nodes), comment counts for index pages, or a "latest comments" sidebar from the same thread object. Doing this in one controller action avoids double API hits.

Paginate with limit + offset. Disqus caps a single request (100), so for long threads either page explicitly or fetch the first N for the page view and lazy-load the rest via the JS embed.

Cache aggressively, and warm it. Every page view that calls the Disqus API costs latency and quota. Point the bundle at a Symfony cache pool and set a TTL of a few minutes to an hour depending on traffic. For high-traffic articles, add a small console command or console-scheduled job that calls the same service to refresh the cache off the request path.

Coexist with the JS embed deliberately. Typical setup: render the fetched comments server-side inside the disqus_thread container, then include the standard Disqus embed script so live users get real-time behavior. Decide up front whether you're doing "SEO-only static render" or "hydrated embed" — mixing both without thinking leaves you with duplicated comment markup.

Testing. In test env, disable/bypass the bundle cache and either mock the Disqus service or point it at a stub; treat "thread doesn't exist yet" as a fixture case rather than an exception. Keep API keys out of test fixtures.

Multiple forums. The forums map lets you target different shortnames from different areas (e.g., docs vs. blog) while keeping one default.

Gotchas and Tips

  • It's a small, lightly maintained bundle — and v4.1.0 is a compatibility bump, not a revival. The release only relaxes the PHP constraint to allow PHP 8; there are no features, no bug fixes, and no Symfony 6/7 widening. Low star count and no dependents mean you're often the first to hit edge cases. Budget a little time for a fork or a service override if Symfony 6/7 compatibility bites.
  • PHP 8 is now installable, but don't assume it was tested against 8.2/8.3. The PR only changed composer.json. Skim the code you depend on (the Twig extension and API wrapper) for anything passing null to non-nullable internal functions or other deprecation-triggering patterns, and run your test suite with deprecations surfaced before you rely on it.
  • New threads aren't errors you can ignore. A thread with zero comments (or a brand-new identifier) can come back empty or as a "not found"-style API response. Wrap the fetch and render a friendly empty state — a 500 on a fresh article is a bad look.
  • Comments are a flat list. Disqus returns comments with a parent reference; the bundle won't rebuild a nested tree for you. If you want threaded display, group by parent yourself before rendering.
  • Treat comment HTML as untrusted. The message field is user-generated HTML. If you render it raw, sanitize it (HTML Purifier or Symfony's HtmlSanitizer) rather than blindly applying |raw.
  • Cache staleness is the #1 support question. A reader will comment, refresh, and not see it. Document the TTL, or wire a Disqus webhook / short-TTL policy so expectations match.
  • API key vs. secret confusion. Read access needs only the public API key. The secret is for OAuth and should never end up in a client bundle or a committed config.
  • Rate limits and quota errors surface as opaque exceptions. Enable the bundle's debug flag (which usually bypasses caching and surfaces raw responses) while diagnosing, and log the underlying API error code rather than swallowing it.
  • Debugging tip: dump the fetched object before rendering — most layout problems are missing/renamed fields in the API payload, not template bugs. Check the Symfony profiler's cache panel to confirm hits/misses on the Disqus pool.
  • Extension points, in order of preference: override the Twig templates → decorate/replace the Disqus service with your own (adds logging, retries, fallbacks) → fork the bundle if a config key or API version is genuinely missing.
  • Don't render comments on pages that don't need them (listings, tag pages). The fetch is a synchronous HTTP call on the request path unless your cache is warm — one forgotten knp_disqus_render in a loop is an easy way to blow your API quota.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib