Giter Club home page Giter Club logo

yii2-seo's Introduction

SEO For Yii2

Library for working with SEO parameters of models

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiier/yii2-seo "*"

or add

"yiier/yii2-seo": "*"

to the require section of your composer.json file.

Configuration

change your config file

<?php
return [
    'components' => [
        'view' => [
            'as seo' => [
                'class' => 'yiier\seo\SeoViewBehavior',
                'names' => [

                    'keywords' => 'blog,forecho',
                    'author' => getenv('APP_NAME'),
                ],
                'properties' => [
                    [
                        'property' => ['title', 'og:title'],
                        'content' => function () {
                            return ' tag1, tag2';
                        },
                    ],
                    'title1' => 'title'
                ],
            ]
        ]
    ]
];

In model file add seo model behavior:

<?php

public function behaviors()
{
    return [
        'seo' => [
            'class' => 'yiier\seo\SeoModelBehavior',
            'names' => [
                'viewport' => function (self $model) {
                    return $model->title . ', tag1, tag2';
                },
                'keywords' => 'blog,forecho',
                'author' => 'author', // model field
            ],
            'properties' => [
                [
                    'property' => ['title', 'og:title'],
                    'content' => function (self $model) {
                        return $model->title . ', tag1, tag2';
                    },
                ],
                'title1' => 'title',
                [
                    'property' => 'description',
                    'content' => function (self $model) {
                        return $model->title . ', tag1, tag2';
                    },
                ],
            ],
        ],
    ];
}

PHPdoc for model:

/**
 * @property \yiier\seo\SeoModelBehavior $seoBehavior
 * @method \yiier\seo\SeoModelBehavior getSeoBehavior()
 */

Change /frontend/views/layouts/main.php:

<?php
/* @var $this \yii\web\View|\yiier\seo\SeoViewBehavior */
?>
<head>
    <!-- Replace default <title> tag -->    
    <title><?= Html::encode($this->title) ?></title>
    <!-- by this line: -->    
    <?php $this->renderMetaTags(); ?>
    ...
</head>

Usage

In "view.php" file for model:

// set SEO:meta data for current page
$this->setSeoData($model->getSeoBehavior());

or in controller:

 Yii::$app->view->setSeoData($model->getSeoBehavior());

Simple url rules example in '/frontend/config/main.php':

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<module>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
        'post/<action:(index|create|update|delete)>' => 'post/<action>',
        'post/<title:[-\w]+>' => 'post/view',
    ],
],

Reference

demisang/yii2-seo

yii2-seo's People

Contributors

forecho avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.