diff --git a/README.md b/README.md
index 99ea62f..e1ff3f9 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,7 @@ Use the `seo()` helper to retrieve the SeoManager instance, on which you can cal
Available methods:
```js
site(string $site)
+url(string $url)
title(string $title)
description(string $description)
image(string $url)
@@ -108,6 +109,31 @@ seo()
->description(default: 'We are a web development agency that ...');
```
+### Extra tags
+
+To add more tags to the head, you can use the `tag()` and `rawTag()` methods:
+
+```php
+seo()->tag('fb:image', asset('foo'));
+seo()->rawTag('');
+```
+
+### Canonical URL
+
+To enable the `og:url` and canonical URL `link` tags, call:
+
+```php
+seo()->withUrl();
+```
+
+This will make the package read from `request()->url()` (= the current URL *without* the query string).
+
+If you wish to change the URL, call `seo()->url()`:
+
+```php
+seo()->url(route('products.show', $this->product));
+```
+
### Modifiers
You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta `
` with `| ArchTech` when it has a non-default value.
@@ -226,15 +252,6 @@ This package is completely flexible, and can be customized either by having its
You can publish the Blade views by running `php artisan vendor:publish --tag=seo-views`.
-### Extra tags
-
-To add more tags to the head, you can use the `tag()` and `rawTag()` methods:
-
-```php
-seo()->tag('fb:image', asset('foo'));
-seo()->rawTag('');
-```
-
### Extensions
To use a custom extension, create a Blade *component* with the desired meta tags. The component should read data using `{{ seo()->get('foo') }}` or `@seo('foo')`.
diff --git a/assets/views/components/meta.blade.php b/assets/views/components/meta.blade.php
index ef747b4..7372978 100644
--- a/assets/views/components/meta.blade.php
+++ b/assets/views/components/meta.blade.php
@@ -4,6 +4,10 @@
@if(seo('title')) @endif
@if(seo('description')) @endif
@if(seo('image')) @endif
+@if(seo('url'))
+
+
+@endif
@foreach(seo()->tags() as $tag)
{!! $tag !!}
diff --git a/src/SEOManager.php b/src/SEOManager.php
index bb900ea..edecd7f 100644
--- a/src/SEOManager.php
+++ b/src/SEOManager.php
@@ -10,6 +10,7 @@ use Illuminate\Support\Str;
/**
* @method $this title(string $title) Set the title.
* @method $this description(string $description) Set the description.
+ * @method $this url(string $url) Set the canonical URL.
* @method $this site(string $site) Set the site name.
* @method $this image(string $url) Set the cover image.
* @method $this twitter(enabled $bool = true) Enable the Twitter extension.
@@ -51,7 +52,10 @@ class SEOManager
/** Get a list of used keys. */
protected function getKeys(): array
{
- return collect(['site', 'title', 'image', 'description', 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description'])
+ return collect([
+ 'site', 'title', 'image', 'description', 'url',
+ 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description'
+ ])
->merge(array_keys($this->defaults))
->merge(array_keys($this->values))
->unique()
@@ -172,6 +176,14 @@ class SEOManager
return $this->set('image', "https://s.useflipp.com/{$template}.png?s={$signature}&v={$query}");
}
+ /** Append canonical URL tags to the document head. */
+ public function withUrl(): static
+ {
+ $this->url(request()->url());
+
+ return $this;
+ }
+
/** Get all extra head tags. */
public function tags(): array
{
diff --git a/tests/Pest/ManagerTest.php b/tests/Pest/ManagerTest.php
index 27b5257..c12a040 100644
--- a/tests/Pest/ManagerTest.php
+++ b/tests/Pest/ManagerTest.php
@@ -94,3 +94,27 @@ test('raw tags can be added to the template', function () {
expect(meta())->toContain('');
});
+
+test('canonical url is not included by default', function () {
+ expect(meta())
+ ->not()->toContain('og:url')
+ ->not()->toContain('canonical');
+});
+
+test('canonical url can be read from request', function () {
+ seo()->withUrl();
+
+ expect(meta())
+ ->toContain('')
+ ->toContain('');
+});
+
+test('canonical url can be changed', function () {
+ seo()->withUrl();
+
+ seo()->url('http://foo.com/bar');
+
+ expect(meta())
+ ->toContain('')
+ ->toContain('');
+});