quaidesbalises / meta
管理您应用程序的元标签
dev-master
2020-08-13 20:43 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2024-09-14 07:02:27 UTC
README
管理您的元标签并在blade布局中显示它们。
安装
composer require quaidesbalises/meta
配置
您可以覆盖默认配置。首先发布配置
php artisan vendor:publish --provider="Qdb\Meta\MetaServiceProvider" --tag=config
这会将默认配置复制到 config/meta.php,您可以在那里编辑它。
配置文件包含默认键和值数组。
'default' => [ /* * Default display keys which needs to be print everytime * if not exists, it takes the most valuable default value * g.e : og:title takes the title default value * g.e : twitter:title takes the og:title default value */ 'keys' => [ 'title', 'description', 'robots', 'og:title', 'og:image', 'og:url', 'og:description', 'twitter:card', 'twitter:title', 'twitter:description', 'twitter:image', 'twitter:url' ], /** * Default values for the given keys */ 'values' => [ 'og:image' => 'images/og-image.jpg', 'twitter:card' => 'summary_large_image', ] ]
用法
设置元标签
Meta::set('title', 'My title') Meta::set('description', 'My description') Meta::set('og:image', '/images/image.jpg')
和 / 或
Meta::set([ 'title' => 'My title', 'description' => 'My description', 'og:image' => '/images/image.jpg' ])
显示元标签
@meta('title')
@meta('description')
@meta('og:image')
和 / 或
@metas(['title', 'description', 'og:image'])
上面的示例将打印
<title>My title</title>
<meta name="description" content="My description">
<meta property="og:image" content="project.test/images/image.jpg">
此外,您还可以一次显示所有元标签
@metas
请注意,此blade指令将处理您配置文件中设置的 默认显示键。您可以自由地添加或删除键到数组中。如果未设置指定键,它将根据您的现有值检索最合理的数据 。
这是一个非常有价值的特性,可以防止您重复为如 og:title
、og:description
、twitter:title
等标签重新编写值。
鉴于配置文件中的默认键,下面是一个实际应用的示例。
示例
模型
class Page extends Model { public function setMetas() { Meta::set([ 'title' => $this->seo_title, 'description' => $this->seo_desc, 'robots' => $this->seo_robots ]); } }
控制器
class PagesController extends Controller { public function show(Page $page) { $page->setMetas(); return view('pages.page', compact('page')); } }
Blade视图
@metas
输出
<title>Home page</title>
<meta name="description" content="This is my home page">
<meta name="robots" content="index, nofollow">
<meta property="og:title" content="Home page">
<meta property="og:description" content="This is my home page">
<meta property="og:image" content="http://project.test/images/og-image.jpg">
<meta property="og:url" content="http://project.test">
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:title" content="Home page">
<meta property="twitter:description" content="This is my home page">
<meta property="twitter:image" content="http://project.test/images/og-image.jpg">
<meta property="twitter:url" content="http://project.test">
根据您配置文件中设置的显示字段和基于可用的og:
和 twitter:
值,这些字段将自动填充。
下一步
- 添加测试
贡献
欢迎提交拉取请求。对于重大更改,请先打开一个问题以讨论您想要更改的内容。
请确保适当地更新测试。