feat: implement structured data support for blog and page components, enhancing SEO and user experience through JSON-LD integration

This commit is contained in:
Ümit Tunç
2026-05-21 21:54:12 +03:00
parent 8675325f50
commit 6d9ffc808b
15 changed files with 509 additions and 176 deletions
+34 -140
View File
@@ -6,12 +6,10 @@ use App\Models\MusicProduction;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Str;
class MusicProductionStructuredData
class MusicProductionStructuredData extends StructuredData
{
public static function forIndex(LengthAwarePaginator $productions, string $pageUrl): array
{
$siteName = setting('site_name', config('app.name'));
$siteUrl = url('/');
$pageName = __('music_productions.meta-index-title');
$pageDescription = __('music_productions.meta-index-description');
@@ -19,45 +17,23 @@ class MusicProductionStructuredData
$offset = ($productions->currentPage() - 1) * $productions->perPage();
foreach ($productions as $index => $production) {
$itemListElements[] = [
'@type' => 'ListItem',
'position' => $offset + $index + 1,
'item' => self::productionSchema($production),
];
$itemListElements[] = self::listItem(
$offset + $index + 1,
self::productionSchema($production),
);
}
$graph = [
self::websiteNode($siteUrl, $siteName),
self::organizationNode($siteUrl, $siteName),
[
return self::wrap([
self::websiteNode(route('music-productions.index') . '?q={search_term_string}'),
self::organizationNode(),
self::webPageNode($pageUrl, $pageName, $pageDescription, [
'@type' => 'CollectionPage',
'@id' => $pageUrl . '#webpage',
'url' => $pageUrl,
'name' => $pageName,
'description' => $pageDescription,
'inLanguage' => app()->getLocale(),
'isPartOf' => ['@id' => $siteUrl . '#website'],
'breadcrumb' => ['@id' => $pageUrl . '#breadcrumb'],
'mainEntity' => ['@id' => $pageUrl . '#itemlist'],
],
[
'@type' => 'BreadcrumbList',
'@id' => $pageUrl . '#breadcrumb',
'itemListElement' => [
[
'@type' => 'ListItem',
'position' => 1,
'name' => __('music_productions.breadcrumb_home'),
'item' => $siteUrl,
],
[
'@type' => 'ListItem',
'position' => 2,
'name' => __('music_productions.nav-music-productions'),
'item' => route('music-productions.index'),
],
],
],
]),
self::breadcrumbNode($pageUrl, [
['name' => __('music_productions.breadcrumb_home'), 'item' => self::siteUrl()],
['name' => __('music_productions.nav-music-productions'), 'item' => route('music-productions.index')],
]),
[
'@type' => 'ItemList',
'@id' => $pageUrl . '#itemlist',
@@ -65,72 +41,36 @@ class MusicProductionStructuredData
'numberOfItems' => $productions->total(),
'itemListElement' => $itemListElements,
],
];
return [
'@context' => 'https://schema.org',
'@graph' => $graph,
];
]);
}
public static function forShow(MusicProduction $production, string $pageUrl): array
{
$siteName = setting('site_name', config('app.name'));
$siteUrl = url('/');
$title = $production->translate('title');
$description = Str::limit(strip_tags((string) $production->translate('content')), 160);
$graph = [
self::websiteNode($siteUrl, $siteName),
self::organizationNode($siteUrl, $siteName),
[
'@type' => 'BreadcrumbList',
'@id' => $pageUrl . '#breadcrumb',
'itemListElement' => [
[
'@type' => 'ListItem',
'position' => 1,
'name' => __('music_productions.breadcrumb_home'),
'item' => $siteUrl,
],
[
'@type' => 'ListItem',
'position' => 2,
'name' => __('music_productions.nav-music-productions'),
'item' => route('music-productions.index'),
],
[
'@type' => 'ListItem',
'position' => 3,
'name' => $title,
'item' => $pageUrl,
],
],
],
array_merge(
[
'@type' => 'WebPage',
'@id' => $pageUrl . '#webpage',
'url' => $pageUrl,
'name' => $title,
'description' => $description,
'inLanguage' => app()->getLocale(),
'isPartOf' => ['@id' => $siteUrl . '#website'],
'breadcrumb' => ['@id' => $pageUrl . '#breadcrumb'],
'mainEntity' => ['@id' => $pageUrl . '#production'],
],
$production->updated_at ? ['dateModified' => $production->updated_at->toIso8601String()] : [],
),
$webPageExtra = [
'mainEntity' => ['@id' => $pageUrl . '#production'],
];
if ($production->updated_at) {
$webPageExtra['dateModified'] = $production->updated_at->toIso8601String();
}
return self::wrap([
self::websiteNode(route('music-productions.index') . '?q={search_term_string}'),
self::organizationNode(),
self::webPageNode($pageUrl, $title, $description, $webPageExtra),
self::breadcrumbNode($pageUrl, [
['name' => __('music_productions.breadcrumb_home'), 'item' => self::siteUrl()],
['name' => __('music_productions.nav-music-productions'), 'item' => route('music-productions.index')],
['name' => $title, 'item' => $pageUrl],
]),
array_merge(
['@id' => $pageUrl . '#production'],
self::productionSchema($production),
),
];
return [
'@context' => 'https://schema.org',
'@graph' => $graph,
];
]);
}
/**
@@ -145,7 +85,7 @@ class MusicProductionStructuredData
];
if ($production->cover_image_url) {
$schema['image'] = $production->cover_image_url;
$schema['image'] = [$production->cover_image_url];
}
$content = strip_tags((string) $production->translate('content'));
@@ -176,50 +116,4 @@ class MusicProductionStructuredData
return $schema;
}
/**
* @return array<string, mixed>
*/
protected static function websiteNode(string $siteUrl, string $siteName): array
{
return [
'@type' => 'WebSite',
'@id' => $siteUrl . '#website',
'url' => $siteUrl,
'name' => $siteName,
'publisher' => ['@id' => $siteUrl . '#organization'],
'potentialAction' => [
'@type' => 'SearchAction',
'target' => [
'@type' => 'EntryPoint',
'urlTemplate' => route('music-productions.index') . '?q={search_term_string}',
],
'query-input' => 'required name=search_term_string',
],
];
}
/**
* @return array<string, mixed>
*/
protected static function organizationNode(string $siteUrl, string $siteName): array
{
$node = [
'@type' => 'Organization',
'@id' => $siteUrl . '#organization',
'name' => $siteName,
'url' => $siteUrl,
];
$logo = setting('site_logo');
if ($logo) {
$logoUrl = str_starts_with($logo, 'http') ? $logo : asset('storage/' . ltrim($logo, '/'));
$node['logo'] = [
'@type' => 'ImageObject',
'url' => $logoUrl,
];
}
return $node;
}
}