Skip to content

Commit 3f45aae

Browse files
committed
add defaults
1 parent 3f1e3c7 commit 3f45aae

File tree

4 files changed

+254
-76
lines changed

4 files changed

+254
-76
lines changed

README.md

Lines changed: 158 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
![laravel-seo](https://repository-images.githubusercontent.com/845966143/6ff7437c-852d-41eb-8b2f-927551506a13)
99

10-
This package offers an extremely flexible and advanced way to manage all of your SEO tags. Unlike other packages that focus on the most basic and common tags, this one implements all the protocols.
10+
This package offers an extremely flexible and advanced way to manage all of your SEO tags.
1111

1212
With this package, you will be able to implement:
1313

@@ -36,63 +36,135 @@ This is the content of the published config file:
3636
```php
3737
return [
3838

39-
/*
40-
|--------------------------------------------------------------------------
41-
| Default Title
42-
|--------------------------------------------------------------------------
43-
|
44-
| This is the default value used for <title>, "og:title", "twitter:title"
45-
|
46-
*/
47-
'title' => env('APP_NAME', 'Laravel'),
48-
49-
/*
50-
|--------------------------------------------------------------------------
51-
| Default Description
52-
|--------------------------------------------------------------------------
53-
|
54-
| This is the default value used for <meta name="description">, <meta property="og:description">, <meta name="twitter:description">
55-
|
56-
*/
57-
'description' => null,
58-
59-
/*
60-
|--------------------------------------------------------------------------
61-
| Default Image path
62-
|--------------------------------------------------------------------------
63-
|
64-
| This is the default value used for <meta property="og:image">, <meta name="twitter:image">
65-
| You can use relative path like "/opengraph.png" or url like "https://example.com/opengraph.png"
66-
|
67-
*/
68-
'image' => null,
69-
70-
/*
71-
|--------------------------------------------------------------------------
72-
| Default Robots
73-
|--------------------------------------------------------------------------
74-
|
75-
| This is the default value used for <meta name="robots">
76-
| See Google documentation here: https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag?hl=fr#directives
77-
|
78-
*/
79-
'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1',
80-
81-
/*
82-
|--------------------------------------------------------------------------
83-
| Default Sitemap path
84-
|--------------------------------------------------------------------------
85-
|
86-
| This is the default value used for <link rel="sitemap">
87-
| You can use relative path like "/sitemap.xml" or url like "https://example.com/sitemap.xml"
88-
|
89-
*/
90-
'sitemap' => null,
39+
'defaults' => [
40+
/*
41+
|--------------------------------------------------------------------------
42+
| Default Title
43+
|--------------------------------------------------------------------------
44+
|
45+
| This is the default value used for <title>, "og:title", "twitter:title"
46+
|
47+
*/
48+
'title' => env('APP_NAME', 'Laravel'),
49+
50+
/*
51+
|--------------------------------------------------------------------------
52+
| Default Description
53+
|--------------------------------------------------------------------------
54+
|
55+
| This is the default value used for <meta name="description">, <meta property="og:description">, <meta name="twitter:description">
56+
|
57+
*/
58+
'description' => null,
59+
60+
/*
61+
|--------------------------------------------------------------------------
62+
| Default Keywords
63+
|--------------------------------------------------------------------------
64+
|
65+
| This is the default value used for <meta name="keywords">
66+
| Type supported: string or array of strings
67+
|
68+
*/
69+
'keywords' => null,
70+
71+
/*
72+
|--------------------------------------------------------------------------
73+
| Default Image path
74+
|--------------------------------------------------------------------------
75+
|
76+
| This is the default value used for <meta property="og:image">, <meta name="twitter:image">
77+
| You can use relative path like "/opengraph.png" or url like "https://example.com/opengraph.png"
78+
|
79+
*/
80+
'image' => null,
81+
82+
/*
83+
|--------------------------------------------------------------------------
84+
| Default Robots
85+
|--------------------------------------------------------------------------
86+
|
87+
| This is the default value used for <meta name="robots">
88+
| See Google documentation here: https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag?hl=fr#directives
89+
|
90+
*/
91+
'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1',
92+
93+
/*
94+
|--------------------------------------------------------------------------
95+
| Default Sitemap path
96+
|--------------------------------------------------------------------------
97+
|
98+
| This is the default value used for <link rel="sitemap">
99+
| You can use relative path like "/sitemap.xml" or url like "https://example.com/sitemap.xml"
100+
|
101+
*/
102+
'sitemap' => null,
103+
],
104+
105+
/**
106+
* @see https://ogp.me/
107+
*/
108+
'opengraph' => [
109+
/*
110+
|--------------------------------------------------------------------------
111+
| Default Site Name
112+
|--------------------------------------------------------------------------
113+
|
114+
| This is the default value used for <meta property="og:site_name" />
115+
| If null: config('app.name') is used.
116+
|
117+
*/
118+
'site_name' => null,
119+
120+
/*
121+
|--------------------------------------------------------------------------
122+
| Default Determiner
123+
|--------------------------------------------------------------------------
124+
|
125+
| This is the default value used for <meta property="og:determiner" />
126+
| Possible values are: a, an, the, "", auto
127+
|
128+
*/
129+
'determiner' => '',
130+
],
131+
132+
/**
133+
* @see https://developer.x.com/en/docs/x-for-websites/cards/overview/abouts-cards
134+
*/
135+
'twitter' => [
136+
/*
137+
|--------------------------------------------------------------------------
138+
| Default Twitter username
139+
|--------------------------------------------------------------------------
140+
|
141+
| This is the default value used for <meta name="twitter:site" />
142+
| Example: @X
143+
|
144+
*/
145+
'site' => null,
146+
],
147+
148+
/**
149+
* @see https://schema.org/WebPage
150+
*/
151+
'schema' => [
152+
/*
153+
|--------------------------------------------------------------------------
154+
| Default WebPage schema
155+
|--------------------------------------------------------------------------
156+
|
157+
| This is the default value used for the schema WebPage
158+
| @see https://schema.org/WebPage for all available properties
159+
|
160+
*/
161+
'defaults' => [],
162+
],
91163

92164
];
93165
```
94166

95-
## Usage
167+
## Introduction
96168

97169
You can display all the SEO tags in your view simply by calling the `seo` function like this:
98170

@@ -102,29 +174,52 @@ You can display all the SEO tags in your view simply by calling the `seo` functi
102174
</head>
103175
```
104176

105-
This function accepts different kinds of arguments, allowing you to take full control over your SEO.
106-
107-
### Basic SEO
177+
This will render all the default tags:
178+
179+
```html
180+
<title>Home</title>
181+
<meta name="robots" content="'.$robots.'" />
182+
<link
183+
rel="canonical"
184+
href="max-snippet:-1,max-image-preview:large,max-video-preview:-1"
185+
/>
186+
<!-- opengraph -->
187+
<meta property="og:title" content="Laravel" />
188+
<meta property="og:url" content="https://exemple.com" />
189+
<meta property="og:locale" content="en" />
190+
<meta property="og:site_name" content="Laravel" />
191+
<meta property="og:type" content="website" />
192+
<!-- twitter / X -->
193+
<meta name="twitter:card" content="summary" />
194+
<meta name="twitter:title" content="Laravel" />
195+
<!-- JSON+LD -->
196+
<script type="application/ld+json">
197+
{
198+
"@context": "https://schema.org",
199+
"@type": "WebPage",
200+
"name": "Home",
201+
"url": "https://example.com"
202+
}
203+
</script>
204+
```
108205

109-
The simplest way to define your SEO tags is with `Elegantly\Seo\SeoData::class`.
110-
This class provides a unified representation of the most common SEO tags (Open Graph, Twitter, etc.).
111-
It will also use the defaults defined in your config.
206+
### Basic Usage
112207

113208
#### From a Controller
114209

115-
Define a `SeoData` object and pass it to the view:
210+
Most of the time, your will want to define you seo tags from a controller
116211

117212
```php
118213
namespace App\Http\Controllers;
119214

120-
use Elegantly\Seo\SeoData;
215+
use \Elegantly\Seo\SeoManager;
121216

122217
class HomeController extends Controller
123218
{
124219
function __invoke()
125220
{
126221
return view('home', [
127-
'seo' => new SeoData(
222+
'seo' => SeoManager::default(
128223
title: "Homepage",
129224
)
130225
]);

src/Schemas/Schema.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,29 @@ public function toTags(): SeoTags
2323
]);
2424
}
2525

26-
public static function default(): self
27-
{
28-
$schema = new self;
29-
30-
return $schema->merge([
26+
public static function default(
27+
?string $title = null,
28+
?string $url = null,
29+
?string $description = null,
30+
?string $image = null,
31+
): self {
32+
$schema = new self([
3133
'@context' => 'https://schema.org',
3234
'@type' => 'WebPage',
3335
'name' => __(config('seo.defaults.title')),
3436
'description' => __(config('seo.defaults.description')),
3537
'image' => config('seo.defaults.image.url'),
3638
'url' => Request::url(),
37-
...config('seo.schema.defaults', []),
38-
])->filter();
39+
]);
40+
41+
return $schema
42+
->merge(config('seo.schema.defaults', []))
43+
->merge(array_filter([
44+
'name' => $title,
45+
'description' => $description,
46+
'image' => $image,
47+
'url' => $url,
48+
]))
49+
->filter();
3950
}
4051
}

src/SeoImage.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Elegantly\Seo;
4+
5+
use Elegantly\Seo\OpenGraph\Image;
6+
use Elegantly\Seo\Twitter\Image as TwitterImage;
7+
8+
class SeoImage
9+
{
10+
public function __construct(
11+
public string $url,
12+
public ?string $secure_url = null,
13+
public ?string $type = null,
14+
public ?string $width = null,
15+
public ?string $height = null,
16+
public ?string $alt = null,
17+
) {}
18+
19+
public function toOpenGraph(): Image
20+
{
21+
return new Image(
22+
url: $this->url,
23+
secure_url: $this->secure_url,
24+
type: $this->type,
25+
width: $this->width,
26+
height: $this->height,
27+
alt: $this->alt,
28+
);
29+
}
30+
31+
public function toTwitter(): TwitterImage
32+
{
33+
return new TwitterImage(
34+
url: $this->secure_url ?? $this->url,
35+
alt: $this->alt
36+
);
37+
}
38+
}

src/SeoManager.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Elegantly\Seo\Contracts\Taggable;
66
use Elegantly\Seo\OpenGraph\OpenGraph;
77
use Elegantly\Seo\Schemas\Schema;
8+
use Elegantly\Seo\Standard\Alternate;
89
use Elegantly\Seo\Standard\StandardData;
910
use Elegantly\Seo\Twitter\Cards\Card;
1011
use Elegantly\Seo\Twitter\Cards\Summary;
@@ -29,13 +30,46 @@ public function current(): static
2930
return $this;
3031
}
3132

32-
public static function default(): self
33-
{
33+
/**
34+
* @param null|string|string[] $keywords
35+
* @param null|Alternate[] $alternates
36+
*/
37+
public static function default(
38+
?string $title = null,
39+
?string $url = null,
40+
?string $description = null,
41+
null|string|array $keywords = null,
42+
?SeoImage $image = null,
43+
?string $robots = null,
44+
?string $sitemap = null,
45+
?array $alternates = null,
46+
): self {
3447
return new self(
35-
standard: StandardData::default(),
36-
opengraph: OpenGraph::default(),
37-
twitter: Summary::default(),
38-
schemas: [Schema::default()],
48+
standard: StandardData::default(
49+
$title,
50+
$url,
51+
$description,
52+
$keywords,
53+
$robots,
54+
$sitemap,
55+
$alternates
56+
),
57+
opengraph: OpenGraph::default(
58+
title: $title,
59+
url: $url,
60+
image: $image?->toOpenGraph(),
61+
),
62+
twitter: Summary::default(
63+
title: $title,
64+
description: $description,
65+
image: $image?->toTwitter(),
66+
),
67+
schemas: [Schema::default(
68+
title: $title,
69+
url: $url,
70+
description: $description,
71+
image: $image?->secure_url ?? $image?->url,
72+
)],
3973
);
4074
}
4175

0 commit comments

Comments
 (0)