, (*1)
Yii2.x Open Graph
, (*2)
Created a new component for Yii2. The Open Graph component for your website, (*3)
Installation
composer require umanskyi31/opengraph
or add, (*4)
"umanskyi31/opengraph": "^3.0.0"
to the require section of your composer.json file., (*5)
Configuration
'components' => [
'opengraph' => [
'class' => 'umanskyi31\opengraph\OpenGraph',
],
// ...
]
Usage
You should add component to controller before rendering view., (*6)
Example:, (*7)
/**
* @var OpenGraph $openGraph
*/
$openGraph = Yii::$app->opengraph;
How to use
Add basic attributes:, (*8)
/**
* @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:, (*9)
/**
* @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:, (*10)
/**
* @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:, (*11)
/**
* @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:, (*12)
/**
* @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:, (*13)
/**
* @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:, (*14)
/**
* @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:, (*15)
/**
* @var OpenGraph $openGraph
*/
$openGraph = Yii::$app->opengraph;
$openGraph->getProfile()
->setGender('Male')
->setFirstName('Vlad')
->setLastName('Umanskyi')
->setUsername('vlad.umanskyi')
->render();
Add video attribute:, (*16)
/**
* @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, (*17)
/**
* @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, (*18)
If you have any issue please let me know., (*19)