PHP code example of pontedilana / open-graph-bundle

1. Go to this page and download the library: Download pontedilana/open-graph-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pontedilana / open-graph-bundle example snippets



// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Pontedilana\OpenGraphBundle\PontedilanaOpenGraphBundle(),
    );
}



namespace App\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;
}



namespace App\OpenGraph;

use App\Entity\BlogPost;
use Pontedilana\OpenGraphBundle\OpenGraph\DocumentWriterInterface;
use Pontedilana\OpenGraphBundle\Map\OpenGraphMapInterface;
use Opengraph\Opengraph;

class BlogPostMap implements OpenGraphMapInterface
{
    /**
     * @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());
    }

    public function supports($data)
    {
        return $entity instanceof BlogPost;
    }
}