ylly/open-graph-bundle

一个 Symfony OpenGraph 扩展包

安装次数: 110

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 2

类型:symfony-bundle

1.2.1 2023-02-07 16:26 UTC

This package is not auto-updated.

Last update: 2024-09-18 22:17:46 UTC


README

TenoloOpenGraphBundle 是一种简单的方法,可以改善您在 Symfony2 应用程序中管理 OpenGraph 的方式。

注意:OpenGraph 是一个由许多网站(Facebook、Twitter、Google 等)使用的标准协议,用于获取有关您内容的更精确信息。

了解更多关于 OpenGraph 的信息

该扩展包的想法是将您的应用程序中的每个实体与一个 OpenGraph 地图 相关联,这是一个能够为您实体创建 OpenGraph 文档的服务。

它还可以与任何其他类型的数据一起工作。

安装

安装非常快速

1. 使用 Composer 下载

通过运行以下命令将扩展包添加到您的 composer.json 文件中:

composer require tenolo/open-graph-bundle

2. 在您的内核中启用它

在您的 app/AppKernel.php 文件中启用扩展包;

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Tenolo\Bundle\OpenGraphBundle\TenoloOpenGraphBundle(),
    );
}

用法

TenoloOpenGraphBundle 将将您的应用程序的 ...

  • 实体 与 ...
  • ... 或 其他数据,如应用程序的数组 与 ...
  • OpenGraph 地图 相关联,这是以 OpenGraph 方式定义这些实体的定义

让我们举一个更好的例子:一篇文章。

您的实体

对于一篇文章,您可能有一个这样的实体

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class BlogPost
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;
}

它的 OpenGraph 地图

与您的实体关联的地图将是一个实现 Tenolo\Bundle\OpenGraphBundle\Map\OpenGraphMapInterface 的类以及该接口的两个必需方法:map(DocumentWriterInterface $document, $data)supports($data)

例如,我们的地图可能看起来像这样

<?php

namespace Acme\DemoBundle\OpenGraph;

use Acme\DemoBundle\Entity\BlogPost;
use Tenolo\Bundle\OpenGraphBundle\OpenGraph\DocumentWriterInterface;
use Tenolo\Bundle\OpenGraphBundle\Map\OpenGraphMapInterface;
use Opengraph\Opengraph;

class BlogPostMap implements OpenGraphMapInterface
{

    /**
     * @inheritdoc
     * @var BlogPost $data
     */
    public function map(DocumentWriterInterface $document, $data)
    {
        $document->append(OpenGraph::OG_SITE_NAME, 'MyBlog');
        $document->append(OpenGraph::OG_TYPE, OpenGraph::TYPE_ARTICLE);
        $document->append(OpenGraph::OG_TITLE, $data->getTitle());
    }

    /**
     * @inheritdoc
     */
    public function supports($data)
    {
        return $entity instanceof BlogPost;
    }
}

supports 方法声明了此地图能够处理哪些类型的实体。 map 方法创建一个表示给定实体的 OpenGraph 文档。

一旦创建,我们还需要将我们的类注册到 OpenGraph 管理器中。为此,我们将需要使用标签 tenolo_open_graph.map

services:
    acme_demo.open_graph.blog_post_map:
        class: Acme\DemoBundle\OpenGraph\BlogPostMap
        tags:
            - { name: tenolo_open_graph.map }

使用地图

我们的地图已注册,因此我们可以在任何需要渲染它的地方使用它。例如,使用 Twig

<html>
    <head>
        <title>Blog post</title>

        {{ opengraph_render(blogPost) }} <!-- blogPost should be an instance of BlogPost -->
    </head>
    <body>
        ...
    </body>
</html>

注意:如果没有任何地图能够处理在 opengraph_render 中提供的实体,将抛出一个 NotSupported 异常。

另一个注意事项:致谢和灵感来自 tgalopin

只是说;)