Skip to content

Commit

Permalink
better config
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 22, 2024
1 parent fbb9ffb commit 5e76563
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

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

'description' => null,

'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1',

'sitemap' => '/sitemap.xml',
'sitemap' => null,

'image' => null,

Expand Down
17 changes: 16 additions & 1 deletion src/SeoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Unified\Image;
use Elegantly\Seo\Unified\SeoUnifiedData;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;

class SeoData extends SeoUnifiedData
Expand All @@ -22,15 +23,29 @@ public function __construct(
?string $title = null,
?string $canonical = null,
public ?string $description = null,
public ?Image $image = null,
public ?string $robots = null,
public ?string $sitemap = null,
public ?array $alternates = null,
public ?Image $image = null,
public ?string $locale = null,
public ?array $schemas = null,
public ?SeoTags $customTags = null,
) {
$this->title = $title ?? config('seo.title') ?? '';
$this->canonical = $canonical ?? URL::current();
$this->description = $description ?? config('seo.description');
$this->robots = $robots ?? config('seo.robots');
$this->sitemap = $sitemap ?? config('seo.sitemap');
$this->image = $image ?? $this->getImageFromConfig();
$this->locale = $locale ?? App::getLocale();
}

public function getImageFromConfig(): ?Image
{
if ($image = config('seo.image')) {
return new Image($image);
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Unified/SeoUnifiedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function __construct(
public string $title,
public string $canonical,
public ?string $description = null,
public ?Image $image = null,
public ?string $robots = null,
public ?string $sitemap = null,
public ?array $alternates = null,
public ?Image $image = null,
public ?string $locale = null,
public ?array $schemas = null,
public ?SeoTags $customTags = null,
Expand Down
9 changes: 9 additions & 0 deletions tests/Features/SeoDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

use Elegantly\Seo\Facades\SeoManager;
use Elegantly\Seo\SeoData;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;

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

$data = new SeoData;

$url = URL::current();
$locale = App::getLocale();
$robots = config('seo.robots');

expect(
$data->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Laravel</title>',
'<meta name="robots" content="'.$robots.'" />',
'<link rel="canonical" href="'.$url.'" />',
'<meta property="og:type" content="website" />',
'<meta property="og:title" content="Laravel" />',
'<meta property="og:url" content="'.$url.'" />',
'<meta property="og:locale" content="'.$locale.'" />',
'<meta name="twitter:card" content="summary" />',
'<meta name="twitter:title" content="Laravel" />',
]));
Expand All @@ -28,15 +33,19 @@
$data = SeoManager::from();

$url = URL::current();
$locale = App::getLocale();
$robots = config('seo.robots');

expect(
$data->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Laravel</title>',
'<meta name="robots" content="'.$robots.'" />',
'<link rel="canonical" href="'.$url.'" />',
'<meta property="og:type" content="website" />',
'<meta property="og:title" content="Laravel" />',
'<meta property="og:url" content="'.$url.'" />',
'<meta property="og:locale" content="'.$locale.'" />',
'<meta name="twitter:card" content="summary" />',
'<meta name="twitter:title" content="Laravel" />',
]));
Expand Down

0 comments on commit 5e76563

Please sign in to comment.