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

ST8*_*T80 4 php schema.org laravel

我正在构建一个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>
Run Code Online (Sandbox Code Playgroud)

现在我想添加不同的参数,取决于我在哪个子页面上。例如,我有一个职位列表页面,我希望每个职位页面都具有以下内容:

{
  "@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"
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

当然,值的变化取决于您所在的工作页面。

我尝试在 -file 中添加脚本job.blade.php,但似乎它被位于 -file 中的脚本app.blade.php覆盖

我该如何解决这个问题?

Mar*_*ark 12

另一种方法是使用像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();
Run Code Online (Sandbox Code Playgroud)

...生成以下 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>
Run Code Online (Sandbox Code Playgroud)

  • 就我个人而言,我在 [viewcomposer](https://laravel.com/docs/8.x/views#view-composers) 中生成代码,然后只需在结束`&lt;/body上方的布局视图中将其打印出来&gt;` 标签如下:`{!! $架构!!}`。 (3认同)

小智 7

您研究过Blade 组件吗?

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

schema.blade.php

<script type="application/ld+json">
    "@context" : "https://schema.org/",
    {{ $slot }}
</script>
Run Code Online (Sandbox Code Playgroud)

job.blade.php

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