mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-02-17 06:43:16 +00:00
✨ Add SEO support with configuration and traits
- Introduced `config/seo.php` to centralize SEO settings. - Implemented `SeoTrait` for dynamic SEO management. - Added `SeoDataAttribute` to set SEO metadata at the class level. - Updated various views to integrate dynamic SEO handling. - Included fallback settings for titles, descriptions, images, and more.
This commit is contained in:
49
app/Attributes/SeoDataAttribute.php
Normal file
49
app/Attributes/SeoDataAttribute.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Attributes;
|
||||
|
||||
use RalphJSmit\Laravel\SEO\Support\SEOData;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)]
|
||||
class SeoDataAttribute
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $key = null, // e.g., 'meetups_index', 'event_show', etc.
|
||||
) {}
|
||||
|
||||
// Centralized SEO data definitions by key as SEOData instances (lazy initialized)
|
||||
private static array $seoDefinitions;
|
||||
|
||||
private static function initDefinitions(): void
|
||||
{
|
||||
self::$seoDefinitions = [
|
||||
'meetups_index' => new SEOData(
|
||||
title: __('Meetups - Übersicht'),
|
||||
description: __('Entdecke alle verfügbaren Meetups in deiner Region.'),
|
||||
),
|
||||
// Add more as needed
|
||||
'default' => new SEOData(
|
||||
title: __('Willkommen'),
|
||||
description: __('Toximalistisches Infotainment für bullische Bitcoiner.'),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
// Static method to get SEO data by key as SEOData instance
|
||||
public static function getData(string $key): SEOData
|
||||
{
|
||||
if (empty(self::$seoDefinitions)) {
|
||||
self::initDefinitions();
|
||||
}
|
||||
return self::$seoDefinitions[$key] ?? self::$seoDefinitions['default'];
|
||||
}
|
||||
|
||||
// If direct SEOData is provided, return it; else fetch by key as SEOData
|
||||
public function resolve(): SEOData
|
||||
{
|
||||
if ($this->key) {
|
||||
return self::getData($this->key);
|
||||
}
|
||||
return self::getData('default'); // Fallback
|
||||
}
|
||||
}
|
||||
31
app/Traits/SeoTrait.php
Normal file
31
app/Traits/SeoTrait.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Attributes\SeoDataAttribute;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
trait SeoTrait
|
||||
{
|
||||
public function mountSeoTrait(): void
|
||||
{
|
||||
$this->setupSeo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup SEO data from attributes and share it globally.
|
||||
*/
|
||||
protected function setupSeo(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this);
|
||||
$attributes = $reflection->getAttributes(SeoDataAttribute::class);
|
||||
|
||||
if (!empty($attributes)) {
|
||||
$seoDataAttribute = $attributes[0]->newInstance();
|
||||
$seoData = $seoDataAttribute->resolve();
|
||||
} else {
|
||||
$seoData = SeoDataAttribute::getData('default');
|
||||
}
|
||||
|
||||
View::share('SEOData', $seoData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user