imw/laravel-meta

laravel的SEO包。

v0.2.0 2020-02-04 22:33 UTC

README

为Laravel应用程序提供基本的页面搜索引擎优化(SEO)。

安装

1 - 安装包

运行:composer require imw/laravel-meta

2 - 添加Laravel Meta提供者。

打开:config/app.php,并在提供者中添加IMW\LaravelMeta\MetaServiceProvider::class

3 - 发布Laravel Meta配置。

运行:php artisan vendor:publish --tag=config --provider="IMW\LaravelMeta\MetaServiceProvider"

用法

使用meta.php配置

您需要通过一个键定义您的元数据,以便以后在blade模板中使用。

<?php
return
[
	'home' =>
	[
		'title|og:title|twitter:title' => 'Home page',
		'description|twitter:description|og:description' => 'Home description',
		'schema' =>
		[
			[
				"@type" => "Organization",
				"url" => "http://www.example.com",
				"logo" => "http://www.example.com/images/logo.png"
			],
			[
				'@type' => 'BreadcrumbList',
				'itemListElement' =>
				[
					[
						'@type' => 'ListItem',
						'position' => 1,
						'name' => 'Home Page',
						'item' => 'http://www.example.com'
					]
				]
			]
		]
	],

	'App\Book' => 'App\Seo\Book'
];

在blade模板中,您只需调用meta指令

@meta('home')

此指令将生成以下结果

<meta name="title" content="Home page">
<meta property="og:title" content="Home page" />
<meta name="twitter:title" content="Home page">
<meta name="description" content="Home description">
<meta name="twitter:description" content="Home description">
<meta property="og:description" content="Home description" />
<script type="application/ld+json">
[
	{
		"@type": "Organization",
		"url": "http:\/\/www.example.com",
		"logo": "http:\/\/www.example.com\/images\/logo.png",
		"@context": "https:\/\/schema.org"
	},
	{
		"@type": "BreadcrumbList",
		"itemListElement": [
			{
				"@type": "ListItem",
				"position": "1",
				"name": "Home Page",
				"item": "http:\/\/www.example.com"
			}
		],
		"@context": "https:\/\/schema.org"
	}
]
</script>

使用模型属性和生成器。

您可以从配置中生成标签,也可以使用生成器,这可能在复杂的模式和大型应用程序中有所帮助。

运行 php artisan make:meta Book,这将生成一个位于app/Seo的类,然后您可以在其中定义元标签和模式,例如

<?php
namespace App\Seo;

use IMW\LaravelMeta\AbstractMetaGenerator;

class Book extends AbstractMetaGenerator
{

	public function generate($book): string
	{
		$this->meta(
		[
			'title' => $book->name,
			'description' => $book->description,
			'schema' =>
			[
				[
					'@type' => 'BreadcrumbList',
					'itemListElement' =>
					[
						[
							'@type' => 'ListItem',
							'position' => 1,
							'name' => 'Books',
							'item' => 'http://www.example.com/books'
						],
						[
							'@type' => 'ListItem',
							'position' => 2,
							'name' => $book->name,
							'item' => 'http://www.example.com/books/1'
						]
					]
				]
			]
		]);

		return $this->toString();
	}
}

在Book Blade文件中

@meta($book)

结果

<meta name="title" content="Book name">
<meta name="description" content="Book description from model">
<script type="application/ld+json">
[
{
	"@type": "BreadcrumbList",
	"itemListElement":
	[
		{
			"@type": "ListItem",
			"position": "1",
			"name": "Books",
			"item": "http:\/\/www.example.com\/books"
		},
		{
			"@type": "ListItem",
			"position": "2",
			"name": "Book name",
			"item": "http:\/\/www.example.com\/books\/1"
		}
	],
	"@context": "https:\/\/schema.org"
}
]
</script>