Open Graph with Yii2

This entry was posted in Yii2 on 2018-07-09 by Eddy
1418 Yii2 OpenGraph Helper

For do that we will make a Helper:

Create the file frontend/components/OpenGraph.php

<?php
namespace frontend\components;
use Yii;
class OpenGraph
{
    public static function generate($properties) {
        foreach($properties as $item => $value)
        {
            Yii::$app->view->registerMetaTag([
                'property' => $item,
                'content' => $value,
            ]);
        }
    }
}

In your controller or view:

use frontend\components\OpenGraph; 

OpenGraph::generate(
    [
        'og:site_name' => 'your site name',
        'og:title' => 'your title',
        'og:description' => 'your description',
        'og:image' => 'your image',
        'og:url' => 'your url',
    ]
);

It’s done

You can add all properties you need, see the list here

Comments

Aucun résultat trouvé.

Write comments

Math, for example, 45-12 = 33

Subscribe Now

You will receive a notification to each new article.

Tag Cloud

  • Composer
  • Yii2
  • OpenGraph
  • Helper
  • Wordpress
  • PhpMyAdmin
  • .htaccess
  • Config
  • Production
  • Git
  • Api
  • Mailchimp
  • Github
  • Vue.js