denisok94/yii-metatag

Generation of Yii2 meta tags.

Installs: 33

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

0.0.8 2022-07-23 18:38 UTC

This package is auto-updated.

Last update: 2024-04-05 18:03:43 UTC


README

Generation of meta tags.

Installation

Run:

composer require --prefer-dist denisok94/yii-metatag
# or
php composer.phar require --prefer-dist denisok94/yii-metatag

or add to the require section of your composer.json file:

"denisok94/yii-metatag": "*"
composer update
# or
php composer.phar update

In the settings (config), where the web.php files are located or config.php specify the name of the site and the main language

$config = [
    'name' => 'Site Name',
    'language' => 'en-EN',
    'basePath' => dirname(__DIR__),
    //...
];

Use

Method Description
setTags Install MetaTag on the page
namespace app\controllers;
use \denisok94\helper\yii2\MetaTag;

class NewsController extends Controller
{
    // ...
    public function actionView($id)
    {
        $model = $this->findModel($id);
        //
        (new MetaTag($this->view))->setTags([
            'title' => $model->title,
            'description' => substr($model->text, 0, 100),
            'keywords' => $model->tags, // string
        ]);
        // or
        $this->view->title = $model->title;
        $meta = new MetaTag($this->view, $model->image->url);
        $meta->setTags([
            'description' => $model->announce,
            'keywords' => implode(', ', $model->tags), // if tags array
        ]);
        //
        return $this->render('view', ['model' => $model]);
    }
}

Specified in action, before `render()'.

$meta = new MetaTag($this->view);
$meta->setTags([
    'nameTag1' => 'valueTag1',
    'nameTag2' => 'valueTag2',
    //...
]);

Set Image

$meta = new MetaTag($this->view, "/image.jpg");

Individual icon(favicon) for the page

// Before
$this->view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => Url::to("/favicon.png", true)]);
// Since MetaTag
$meta = new MetaTag($this->view, null, "/favicon.png");