inblank/yii2-seobility 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

inblank/yii2-seobility

Composer 安装命令:

composer require inblank/yii2-seobility

包简介

Behavior for Yii2 to manage SEO data for ActiveRecord

README 文档

README

Build Status Packagist Version Code Coverage Code Quality GitHub license

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

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.

Each SEO data entry contains the fields: title, keywords and description.

Installation

The preferred way to install this extension is through composer.

Navigate to the project folder and run the console command:

$ composer require inblank/yii2-seobility

or add:

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

to the require section of your composer.json file.

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.

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()

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}}.

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

The query uses the MySQL syntax

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

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.

Usage

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

Setting default SEO data

Default SEO data have condition=0

To setting default SEO data:

$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.

Note: If the model has not passed validation and was not saved SEO data will not be saved too.

Setting SEO data with condition

To setting SEO data with condition:

$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.

Getting default SEO data

To getting default SEO data:

$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.

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

Getting SEO data with condition

To getting SEO data with condition:

$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.

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

// 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:

$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.

Remove default SEO data

To remove default SEO data:

$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.

Remove SEO data with condition

To remove SEO data with condition:

$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.

Remove all SEO data for model

To remove all SEO data for model:

$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.

inblank/yii2-seobility 适用场景与选型建议

inblank/yii2-seobility 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「html」 「seo」 「Behavior」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 inblank/yii2-seobility 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 inblank/yii2-seobility 我们能提供哪些服务?
定制开发 / 二次开发

基于 inblank/yii2-seobility 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 52
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-14