php - Laravel 如何在多个页面上包含 Schema.org 结构化数据
问题描述
我正在构建一个laravel
应用程序,我想在其中包含一些schema.org
结构化数据。在我的app.blade.php
- 文件中,这是一个布局文件 - 我已经包含了这个:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"name": "thecompany.com",
"alternateName": "the company",
"url": "{{ route('home') }}"
}
</script>
现在我想添加不同的参数,等待我在哪个子页面。例如,我有一个工作列表页面,我希望每个工作页面都有类似的内容:
{
"@context" : "https://schema.org/",
"@type" : "JobPosting",
"title" : "Software Engineer",
"description" : "<p>Become our next developer.</p>",
"employmentType" : "CONTRACTOR",
"hiringOrganization" : {
"@type" : "Organization",
"name" : "The Company",
"sameAs" : "http://www.google.com",
"logo" : "http://www.example.com/images/logo.png",
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": 40.00,
"unitText": "HOUR"
}
}
}
并且值当然会在您所在的工作页面上发生变化。
我试图在job.blade.php
-file 中添加脚本,但它似乎被位于app.blade.php
-file中的脚本覆盖
我该如何解决这个问题?
解决方案
另一种方法是为 Laravel使用像 Schema.org 生成器这样的包
这使您可以编写如下代码:
use Spatie\SchemaOrg\Schema;
$localBusiness = Schema::localBusiness()
->name('Spatie')
->email('info@spatie.be')
->contactPoint(Schema::contactPoint()->areaServed('Worldwide'));
echo $localBusiness->toScript();
...生成以下 JSON-LD:
<script type="application/ld+json">
{
"@context": "http:\/\/schema.org",
"@type": "LocalBusiness",
"name": "Spatie",
"email": "info@spatie.be",
"contactPoint": {
"@type": "ContactPoint",
"areaServed": "Worldwide"
}
}
</script>
推荐阅读
- javascript - 从从 chrome 扩展的后台 JS 获取响应的函数返回一个承诺
- python - 关于使用 py bs4 进行网页抓取的问题
- c# - AutoMapper map from collection of IConfigurationSection with complex mapping
- javascript - 将带有渐变笔划的 svg 导入到 paper.js 项目中
- json - 是否可以根据调用的 API 端点启用/禁用自定义反序列化器?
- java - 将 JSON 值转换为相应的数据类型
- mosquitto - 如何为 Jelastic Mosquitto 持久化配置/数据
- mysql - Mysql更改列的默认值
- sql - 选择与第二列中的多个值匹配的所有值
- php - 如何在 Laravel 5.8 中发送电子邮件