knplabs/knp-disqus-bundle

安装数: 78,392

依赖: 0

推荐: 0

安全: 0

星标: 62

关注者: 31

分支: 16

开放问题: 6

类型:symfony-bundle

v4.1.0 2022-01-19 14:49 UTC

This package is auto-updated.

Last update: 2024-09-19 20:45:23 UTC


README

如果你在网站上使用 Disqus 进行评论,评论将通过JavaScript动态加载,这可能会对SEO产生负面影响。

此捆绑包将使用Disqus API获取评论,以便您可以在页面中包含它们……在用Disqus JavaScript小部件替换评论 div 之前。

这样,您既可以使用JavaScript小部件,又可以使用适合机器人的评论。

Build Status

knpbundles.com

要求

安装

使用 composer,运行

composer require knplabs/knp-disqus-bundle

如果您不使用Symfony Flex,那么您还需要在您的 bundles.php 文件中启用 Knp\Bundle\DisqusBundle\KnpDisqusBundle

接下来,创建一个 config/packages/knp_disqus.yaml 文件

# config/packages/knp_disqus.yaml
knp_disqus:
    api_key: '%env(DISQUS_API_KEY)%'

最后,在您的 .env.env.local 文件中配置 DISQUS_API_KEY

# .env

DISQUS_API_KEY=ABC123

用法

在您的Twig模板中

{{ knp_disqus_render('your_disqus_shortname', {'identifier': '/december-2010/the-best-day-of-my-life/', 'limit': 10}) }}

您也可以显示特定语言的评论

{{ knp_disqus_render('your_disqus_shortname', {'identifier': '/december-2010/the-best-day-of-my-life/', 'language': 'de_formal'}) }}

或在控制器中

use Knp\Bundle\DisqusBundle\Client\DisqusClientInterface;

public function somePage(DisqusClientInterface $disqusClient)
{
    // ...

    $comments = $disqusClient->fetch('your_disqus_shortname', [
        'identifier' => '/december-2010/the-best-day-of-my-life/',
        'limit'      => 10, // Default limit is set to max. value for Disqus (100 entries)
    //    'language'   => 'de_formal', // You can fetch comments only for specific language
    ]);

    return $this->render('articles/somePage.html.twig', [
        'comments' => $comments,
    ]);
}

添加新评论的回调

如果您想在添加新评论时调用JavaScript函数(例如触发某些分析),首先,在某个地方创建一个全局JavaScript函数(即附加到 windows 对象的函数)

window.onNewComment = function(comment) {
    console.log(comment);
}

接下来,在渲染时传递函数名

{{ knp_disqus_render('your_disqus_shortname', {
    'identifier': '/december-2010/the-best-day-of-my-life/',
    'limit': 10,
    'newCommentCallbackFunctionName': 'onNewComment'
}) }}

SSO身份验证(可选)

如果您想通过 Disqus SSO 机制管理身份验证,您必须在配置中添加应用程序密钥,并传递用户信息(id、用户名、电子邮件),这将从中生成HMAC有效载荷,以及特定的登录/注销服务信息到助手。确保设置您的Disqus论坛以使用SSO并允许本地域名(用于开发目的)。

要使用SSO身份验证,请将 sso.user 信息传递到参数中,以告诉Disqus哪个用户已登录。传递一个 id 为空的用户,以强制Disqus注销用户,或分别告诉Disqus没有用户通过SSO登录。在 sso.service 参数中添加有关您的SSO身份验证服务(登录/注销URL、图标等)的信息。有关更多信息,请参阅 Disqus SSO文档

{{ knp_disqus_render(
    'dolor-sid',
    {
        'identifier': '/december-2010/the-best-day-of-my-life/',
        'limit': 100,
        'sso': {
            'user': {
                'id' : 'test',
                'username' : 'John Doe',
                'email': 'john.doe@example.com',
            },
            'service': {
                'name': 'MyAuthServiceProvider',
                'icon': 'http://example.com/favicon.png',
                'button': 'http://example.com/images/samplenews.gif',
                'url': 'http://example.com/login/',
                'logout': 'http://example.com/logout/',
                'width': '400',
                'height': '400'
            }
        }
    },
    'KnpDisqusBundle::list.html.twig' )
}}

配置

# config/packages/knp_disqus.yaml
knp_disqus:
    api_key: 'your-disqus-api-key'
    secret_key: 'disqus-api-key' # optional, for SSO auth only

享受吧!