我正在构建一个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
文件中的脚本覆盖
我该如何解决?
答案 0 :(得分:3)
一种替代方法是使用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>
答案 1 :(得分:1)
您是否研究过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