am2studio/laravel-seo-meta

Laravel SEO 元标签

1.0.5 2017-10-25 11:33 UTC

This package is not auto-updated.

Last update: 2024-09-19 13:07:29 UTC


README

用于处理 Laravel 应用程序中的 SEO 元标签的包

安装

通过 Composer

$ composer require am2studio/laravel-seo-meta

使用方法

首次运行此包的迁移(src/migration/)

Schema::create('seo_metas', function (Blueprint $table) {
	$table->increments('id');
	$table->string('model_type');
	$table->integer('model_id')->unsigned();
	$table->text('key');
	$table->text('value');

	$table->timestamps();
});

对于每个使用 SEO 元标签的模型,添加 trait "SeoMetaTrait" 并实现接口 "SeoMetaInterface"

use AM2Studio\Laravel\SeoMeta\SeoMetaTrait;
use AM2Studio\Laravel\SeoMeta\SeoMetaInterface;

class User implements  SeoMetaInterface
{
    use SeoMetaTrait;

"SeoMetaInterface" 接口有 2 个模型需要实现的功能函数:"seoMetasConfig()" 和 "seoMetas()"

"seoMetasConfig()" 是模型的元数据配置

"seoMetas()" 是模型与 "seoMetas" 的 "hasMany" 关系

public function seoMetasConfig()
{
	return [
		'title'         => ['generator' => 'example.com - '. $this->title],
		'description'   => ['generator' => 'green-rush.com - '. $this->title . ' - ' . $this->short_description,],
		'keywords'      => ['generator' => 'greenrush, product, ' . $this->title . ', ' . $this->short_description,
		'edit'=> false],
		'og:image'      => ['generator' => ["http://i.stack.imgur.com/hEobN.jpg", "http://i.stack.imgur.com/hEobN2.jpg"]],
		'twitter:site'  => [],
	];
}

public function seoMetas()
{
	return $this->hasMany(SeoMeta::class, 'model_id')->where(['model_type' => __CLASS__]);
}

每个想要模型使用的 SEO 元标签都必须在此处定义。可能的 SEO 元标签列表

title						-> string
description					-> string
keywords           			-> string
canonical      				-> string
article:published_time		-> string
article:section				-> string
og:description				-> string
og:title					-> string
og:url               		-> string
og:type             		-> string
og:locale           		-> string
og:locale:alternate			-> array
og:site_name        		-> string
og:image         			-> array
og:image:url       			-> array
og:image:size       		-> string
twitter:card       			-> string
twitter:title      			-> string
twitter:site				-> string

对于配置中定义的每个 SEO 元标签,定义生成器(如何生成 SEO 元标签)和编辑(如果 SEO 元标签可以编辑或将在模型保存时始终生成,默认为 true)

最后将 "seoMeta" 添加到 Model -> fillable

protected $fillable = [
	...
	'seoMeta'
];

在模型上显示 SEO 元数据表单

{!! \AM2Studio\Laravel\SeoMeta\SeoMetaHelper::form($product) !!}

在控制器中设置数据

public function __construct()
    {
        parent::__construct();
        SeoMetaHelper::setSeoMeta(['title' => 'Default title __construct .']);
    }

    public function index()
    {
		SeoMetaHelper::setSeoMeta(['description' => 'Default description index .']);
        return $this->view('index');
    }
	
	public function show($product)
    {
        SeoMetaHelper::setSeoMeta($product->getSeoMeta());
		
		return $this->view('show', compact('product'));
	}

在视图中显示元标签

{{
    \AM2Studio\Laravel\SeoMeta\SeoMetaHelper::render([
        //alternative data if is not set anywhere
        'keywords' => 'example.com -> frontend layout'
    ])
}}

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

贡献

请参阅 CONTRIBUTINGCONDUCT 了解详细信息。

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。