Skip to content

vumanskyi/yii2-opengraph

Repository files navigation


Yii2.x Open Graph

Build Status StyleCI Total Downloads Latest Stable Version License: MIT

Created a new component for Yii2. The Open Graph component for your website

Installation

composer require umanskyi31/opengraph

or add

"umanskyi31/opengraph": "^3.0.0"

to the require section of your composer.json file.

Configuration

'components' => [
     'opengraph' => [
        'class' => 'umanskyi31\opengraph\OpenGraph',
     ],
     // ...
]

Usage

You should add component to controller before rendering view.

Example:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;

How to use

Add basic attributes:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getBasic()
       ->setUrl('https://umanskyi.com') 
       ->setTitle('My_Article_Title')
       ->setDescription('My_Article_Description')
       ->setSiteName('My_Site_Name')
       ->setLocale('pl_PL')
       ->setLocalAlternate(['fr_FR', 'en_US'])
       ->render();

Add image attributes:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getImage()
       ->setUrl('https://umanskyi.com/logo.png')
       ->setAttributes([
           'secure_url' => 'https://umanskyi.com/logo.png',
           'width'      => 100,
           'height'     => 100,
           'alt'        => "Logo",
       ])
       ->render();

If necessary, an array of images also possible to add the next code:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getImage()
       ->setUrl('https://umanskyi.com/logo.png')
       ->setAttributes([
           'secure_url' => 'https://umanskyi.com/logo.png',
           'width'      => 100,
           'height'     => 100,
           'alt'        => "Logo",
       ])
       ->render();     
 
 $openGraph->getImage()
       ->setUrl('https://umanskyi.com/small_logo.png')
       ->setAttributes([
           'secure_url' => 'https://umanskyi.com/small_logo.png',
           'width'      => 50,
           'height'     => 50,
           'alt'        => "small logo",
       ])
       ->render();
       

Add article attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
        
 $openGraph->getArticle()
       ->setAuthor(['http://examples.opengraphprotocol.us/profile.html'])
       ->setTag(['Test_TAG'])
       ->setSection('Front page')
       ->setPublishTime(new \DateTime('2010-10-11'))
       ->render();

Add audio attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;

 $openGraph->getAudio()
       ->setAttributes([
         'secure_url' => 'https://umanskyi.com/media/audio/250hz.mp3',
         'type' => 'audio/mpeg'
       ])
       ->setUrl('https://d72cgtgi6hvvl.cloudfront.net/media/audio/250hz.mp3')
       ->render();

Add book attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getBook()
      ->setReleaseDate(new \DateTime('2011-10-10'))
      ->setTag(['Apple', 'New'])
      ->setAuthor(['http://umanskyi.com/profile.html'])
      ->setIsbn(1451648537)
      ->render();

Add music attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getMusic()
      ->setReleaseDate(new \DateTime('2016-01-16'))
      ->setDuration(236)
      ->setAttrAlbum([
          'album:track' => 2
      ])
      ->setMusician([
          'http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d',
          'http://open.spotify.com/artist/1dfeR4HaWDbWqFirlsag1d'
      ])
      ->render();

Add profile attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
    
 $openGraph->getProfile()
      ->setGender('Male')
      ->setFirstName('Vlad')
      ->setLastName('Umanskyi')
      ->setUsername('vlad.umanskyi')
      ->render();   
            

Add video attribute:

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
 
 $openGraph->getVideo()
      ->setUrl('https://umanskyi.com/strobe/FlashMediaPlayback.swf')
      ->setAttributes([
          'width' => 450,
          'height' => 350,
          'secure_url' => 'https://umanskyi.com/strobe/FlashMediaPlayback.swf',
          'type' => 'application/x-shockwave-flash'
      ])
      ->setAdditionalAttributes([
          'tag' => 'train',
          'release_date' => '1980-10-02'
      ])
      ->render();

The current version also has a configuration Twitter Card

/**
 * @var OpenGraph $openGraph
 */
 $openGraph = Yii::$app->opengraph;
        
 $openGraph->useTwitterCard()
    ->setCard('summary')
    ->setSite('https://umanskyi.com')
    ->setCreator('Vlad Umanskyi')
    ->render();

If you want to add own configuration or override some tag, you must implement umanskyi31\opengraph\Configuration and add to Yii container. Some example you can find here umanskyi31\opengraph\OpenGraphConfiguration


If you have any issue please let me know.