首页 > 解决方案 > 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中的脚本覆盖

我该如何解决这个问题?

标签: phplaravelschema.org

解决方案


另一种方法是为 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>

推荐阅读