2017 © Pedro Peláez
 

yii2-extension yii2-seobility

Behavior for Yii2 to manage SEO data for ActiveRecord

image

inblank/yii2-seobility

Behavior for Yii2 to manage SEO data for ActiveRecord

  • Sunday, May 15, 2016
  • by inblank
  • Repository
  • 1 Watchers
  • 0 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 3 Versions
  • 7 % Grown

The README.md

Behavior for Yii2 to manage SEO data for ActiveRecord

Build Status Packagist Version Code Coverage Code Quality GitHub license, (*1)

Русская версия этого документа доступна здесь., (*2)

Behavior yii2-seobility for Yii2 allows you to manage SEO data for ActiveRecord models. For each model, you can store multiple records with SEO data and select according to the condition. If data with the condition not found, then will returned the default data, and if them not, will returned the data with empty values., (*3)

Each SEO data entry contains the fields: title, keywords and description., (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Navigate to the project folder and run the console command:, (*6)

$ composer require inblank/yii2-seobility

or add:, (*7)

"inblank/yii2-seobility": "~0.1"

to the require section of your composer.json file., (*8)

Configuring

Database

To storing SEO data the behavior uses an ActiveRecord model's database connection. Behavior does not create and does not check for required tables. Tables must be created before using., (*9)

The name of the table to store and retrieve data, the behavior has on the basis of the table name of the ActiveRecord model to which is attached. The name is created by adding the suffix _seo to the table name of the ActiveRecord model. To get the table name of the model the model uses the method ActiveRecord::tableName(), (*10)

Examples: - If the model uses a table model, the behaviour will use the table model_seo. - If the model uses a table {{%model}}, the behaviour will use the table {{%model_seo}}., (*11)

To create a table use the following SQL query, replacing model_seo by required table name:, (*12)

The query uses the MySQL syntax, (*13)

CREATE TABLE `model_seo`(
    `model_id` INT NOT NULL DEFAULT 0,
    `condition` INT NOT NULL DEFAULT 0,
    `title` TEXT NOT NULL,
    `keywords` TEXT NOT NULL,
    `description` TEXT NOT NULL,

    PRIMARY KEY (`model_id`, `condition`)
);

Model

To use a behavior just attach it to the ActiveRecord model as specified in the Yii2 documentation, (*14)

use inblank\seobility\SeobilityBehavior;

/**
 * ...
 */
class Model extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            SeobilityBehavior::className(),
        ];
    }
}

If you correctly created a table to store the SEO data, configuring is complete., (*15)

Usage

After successful configuration, you can use behavior methods to manage SEO data of ActiveRecord models., (*16)

Setting default SEO data

Default SEO data have condition=0, (*17)

To setting default SEO data:, (*18)

$model = Model::findOne(1);

// set data by the behavior method
$model->setSeobility([
    'title' => 'SEO title for model', 
    'keywords' => 'model, seo, keywords, yii2', 
    'description' => 'Simple model with SEO data' 
]);

// ... or by direct access
$model->seobility = [
    'title' => 'SEO title for model', 
    'keywords' => 'model, seo, keywords, yii2', 
    'description' => 'Simple model with SEO data' 
];

// not necessarily set all field for SEO data
// code below set only `title` field
$model->seobility = [
    'title' => 'Chnaged SEO title', 
];

// need to save model to store all setting SEO data
$model->save();

After saving the model, the default SEO data will contain defined values and these will be available at any time., (*19)

Note: If the model has not passed validation and was not saved SEO data will not be saved too., (*20)

Setting SEO data with condition

To setting SEO data with condition:, (*21)

$model = Model::findOne(1);

// setting a data with condition is only possible through the method of behavior
// set SEO data for condition=1
$model->setSeobility([
    'title' => 'Another SEO title', 
    'keywords' => 'model, seo, keywords, yii2', 
    'description' => 'This description is different' 
], 1);

// need to save model to store all setting SEO data
$model->save();

Note: If the model has not passed validation and was not saved SEO data will not be saved too., (*22)

Getting default SEO data

To getting default SEO data:, (*23)

$model = Model::findOne(1);

// getting default data through the method of behavior
$seo = $model->getSeobility();

// ... or by direct access
$seo = $model->seobility;

After getting the data, the variable $seo will contain an array with keys title, keywords and description., (*24)

Data will be obtained even if they were not specified. In this case, all array fields will contain empty value., (*25)

Getting SEO data with condition

To getting SEO data with condition:, (*26)

$model = Model::findOne(1);

// getting data with condition is only possible through the method of behavior
$seo = $model->getSeobility(1);

Will be obtained SEO data with condition=1, and if no such data will be getting the default SEO data, or empty if no default data., (*27)

Through the method parameters, you can specify what data to obtain if the requested data is not found., (*28)

// not get the default data if data with `condition=1` not found 
// i.e. if the data with `condition=1` will not be found, it returns 
// an array with empty values
$seo = $model->getSeobility(1, false);

// get data with `condition=2` if not found  data with `condition=1` 
// and if not found data with `condition=1` will return an array with empty values
$seo = $model->getSeobility(1, true, 2);

Getting all SEO data

To getting all SEO data:, (*29)

$model = Model::findOne(1);

// getting all data
$seo = $model->getAllSeobility();

After getting the data, the variable $seo will contain an array with all the SEO data. Indices of elements in the array are the values of the condition., (*30)

Remove default SEO data

To remove default SEO data:, (*31)

$model = Model::findOne(1);

// remove default data
$seo = $model->deleteSeobility();

Attention: Be careful, the removal happens immediately and does not require $model->save() method. Removed data cannot be restore., (*32)

Remove SEO data with condition

To remove SEO data with condition:, (*33)

$model = Model::findOne(1);

// remove data with condition=1
$seo = $model->deleteSeobility(1);

Attention: Be careful, the removal happens immediately and does not require $model->save() method. Removed data cannot be restore., (*34)

Remove all SEO data for model

To remove all SEO data for model:, (*35)

$model = Model::findOne(1);

// remove ALL model's SEO data
$seo = $model->deleteAllSeobility();

Attention: Be careful, the removal happens immediately and does not require $model->save() method. Removed data cannot be restore., (*36)

The Versions

15/05 2016

dev-master

9999999-dev https://github.com/inblank/yii2-seobility

Behavior for Yii2 to manage SEO data for ActiveRecord

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pavel Aleksandrov

yii2 html behavior seo

15/05 2016

0.1.1

0.1.1.0 https://github.com/inblank/yii2-seobility

Behavior for Yii 2 to support work with SEO data in ActiveRecord

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pavel Aleksandrov

yii2 html behavior seo

14/05 2016

0.1.0

0.1.0.0 https://github.com/inblank/yii2-seobility

Behavior for Yii 2 to support work with SEO data in ActiveRecord

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pavel Aleksandrov

yii2 html behavior seo