Skip to content

Commit 5e76563

Browse files
committed
better config
1 parent fbb9ffb commit 5e76563

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

config/seo.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
'title' => env('APP_NAME', 'Laravel'),
77

8+
'description' => null,
9+
810
'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1',
911

10-
'sitemap' => '/sitemap.xml',
12+
'sitemap' => null,
1113

1214
'image' => null,
1315

src/SeoData.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Elegantly\Seo\Standard\Alternate;
77
use Elegantly\Seo\Unified\Image;
88
use Elegantly\Seo\Unified\SeoUnifiedData;
9+
use Illuminate\Support\Facades\App;
910
use Illuminate\Support\Facades\URL;
1011

1112
class SeoData extends SeoUnifiedData
@@ -22,15 +23,29 @@ public function __construct(
2223
?string $title = null,
2324
?string $canonical = null,
2425
public ?string $description = null,
26+
public ?Image $image = null,
2527
public ?string $robots = null,
2628
public ?string $sitemap = null,
2729
public ?array $alternates = null,
28-
public ?Image $image = null,
2930
public ?string $locale = null,
3031
public ?array $schemas = null,
3132
public ?SeoTags $customTags = null,
3233
) {
3334
$this->title = $title ?? config('seo.title') ?? '';
3435
$this->canonical = $canonical ?? URL::current();
36+
$this->description = $description ?? config('seo.description');
37+
$this->robots = $robots ?? config('seo.robots');
38+
$this->sitemap = $sitemap ?? config('seo.sitemap');
39+
$this->image = $image ?? $this->getImageFromConfig();
40+
$this->locale = $locale ?? App::getLocale();
41+
}
42+
43+
public function getImageFromConfig(): ?Image
44+
{
45+
if ($image = config('seo.image')) {
46+
return new Image($image);
47+
}
48+
49+
return null;
3550
}
3651
}

src/Unified/SeoUnifiedData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public function __construct(
2626
public string $title,
2727
public string $canonical,
2828
public ?string $description = null,
29+
public ?Image $image = null,
2930
public ?string $robots = null,
3031
public ?string $sitemap = null,
3132
public ?array $alternates = null,
32-
public ?Image $image = null,
3333
public ?string $locale = null,
3434
public ?array $schemas = null,
3535
public ?SeoTags $customTags = null,

tests/Features/SeoDataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22

33
use Elegantly\Seo\Facades\SeoManager;
44
use Elegantly\Seo\SeoData;
5+
use Illuminate\Support\Facades\App;
56
use Illuminate\Support\Facades\URL;
67

78
it('renders default seo from config', function () {
89

910
$data = new SeoData;
1011

1112
$url = URL::current();
13+
$locale = App::getLocale();
14+
$robots = config('seo.robots');
1215

1316
expect(
1417
$data->toTags()->toHtml()
1518
)->toBe(implode("\n", [
1619
'<title >Laravel</title>',
20+
'<meta name="robots" content="'.$robots.'" />',
1721
'<link rel="canonical" href="'.$url.'" />',
1822
'<meta property="og:type" content="website" />',
1923
'<meta property="og:title" content="Laravel" />',
2024
'<meta property="og:url" content="'.$url.'" />',
25+
'<meta property="og:locale" content="'.$locale.'" />',
2126
'<meta name="twitter:card" content="summary" />',
2227
'<meta name="twitter:title" content="Laravel" />',
2328
]));
@@ -28,15 +33,19 @@
2833
$data = SeoManager::from();
2934

3035
$url = URL::current();
36+
$locale = App::getLocale();
37+
$robots = config('seo.robots');
3138

3239
expect(
3340
$data->toTags()->toHtml()
3441
)->toBe(implode("\n", [
3542
'<title >Laravel</title>',
43+
'<meta name="robots" content="'.$robots.'" />',
3644
'<link rel="canonical" href="'.$url.'" />',
3745
'<meta property="og:type" content="website" />',
3846
'<meta property="og:title" content="Laravel" />',
3947
'<meta property="og:url" content="'.$url.'" />',
48+
'<meta property="og:locale" content="'.$locale.'" />',
4049
'<meta name="twitter:card" content="summary" />',
4150
'<meta name="twitter:title" content="Laravel" />',
4251
]));

0 commit comments

Comments
 (0)