Laravel 如何在多个页面上包含 Schema.org 结构化数据

Laravel How to include Schema.org structured data on multiple pages

我正在构建一个 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 文件中添加脚本,但它似乎被位于 app.blade.php 文件

中的脚本覆盖了

我该如何解决这个问题?

你调查过 Blade components 了吗?

您可以构建一个具有一些默认值的架构组件并将其包含在您的工作网站上:

schema.blade.php

<script type="application/ld+json">
    "@context" : "https://schema.org/",
    {{ $slot }}
</script>

job.blade.php

@component('schema')
    "@type" : "JobPosting",
    "title" : "Software Engineer",
    "description" : "<p>Become our next developer.</p>",
    ....
@endcomponent

另一种方法是使用像 Schema.org generator for Laravel

这样的包

这允许您编写如下代码:

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>