Skip to content

Yii2 component that generates OpenGraph tags for your models

License

Notifications You must be signed in to change notification settings

rikipm/yii2-oggenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

yii2-oggenerator

Its simple Yii2 component that generates OpenGraph tags for your models pages

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist "rikipm/yii2-oggenerator" "*"

or add

"rikipm/yii2-oggenerator" : "*"

to the require section of your application's composer.json file.

Usage

Add new component to components section of your application config and configure it

'oggenerator'=>[
            'class' => 'rikipm\oggenerator',

            'locale' => 'en-US', //Its "og:locale" tag for all pages
            'site_name' => 'MySite',  //Its "og:site_name" tag for all pages

            'attr_title' => 'title',
            'attr_type' => 'type',
            'attr_image' => 'image',
            'attr_image_alt' => 'image_alt',
            'attr_description' => 'description',
        ],

attr_ options will determine what attribute from model will be used for content of tag. In example 'og:title' will assigned $model->title value, 'og:image' will assigned $model->image value, etc.

og:url ,og:image:type, og:image:width and og:image:height tags will be generated automatically.

In controller that display your page add Yii::$app->oggenerator->generate($model);

For example:

public function actionView($id)
{
  $model = Model::findOne(['id' => $id]);

  Yii::$app->oggenerator->generate($model);
  return $this->render('view', ['model' => $model]);
}

Attention!

og:image must be absolute url. You can use getters in model class to generate value

For example:

public getImage()
{
  return Url::home().'/upload/'.$model->image_file;
}
``

About

Yii2 component that generates OpenGraph tags for your models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages