mirror of
https://github.com/archtechx/laravel-seo.git
synced 2025-12-12 09:54:03 +00:00
Canonical URLs, fix #1
This commit is contained in:
parent
6df030c28d
commit
db7269ff28
4 changed files with 67 additions and 10 deletions
35
README.md
35
README.md
|
|
@ -48,6 +48,7 @@ Use the `seo()` helper to retrieve the SeoManager instance, on which you can cal
|
||||||
Available methods:
|
Available methods:
|
||||||
```js
|
```js
|
||||||
site(string $site)
|
site(string $site)
|
||||||
|
url(string $url)
|
||||||
title(string $title)
|
title(string $title)
|
||||||
description(string $description)
|
description(string $description)
|
||||||
image(string $url)
|
image(string $url)
|
||||||
|
|
@ -108,6 +109,31 @@ seo()
|
||||||
->description(default: 'We are a web development agency that ...');
|
->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('<meta property="fb:url" content="bar" />');
|
||||||
|
```
|
||||||
|
|
||||||
|
### 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
|
### Modifiers
|
||||||
|
|
||||||
You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta `<title>` with `| ArchTech` when it has a non-default value.
|
You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta `<title>` 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`.
|
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('<meta property="fb:url" content="bar" />');
|
|
||||||
```
|
|
||||||
|
|
||||||
### Extensions
|
### 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')`.
|
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')`.
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@
|
||||||
@if(seo('title')) <meta property="og:title" content="@seo('title')" /> @endif
|
@if(seo('title')) <meta property="og:title" content="@seo('title')" /> @endif
|
||||||
@if(seo('description')) <meta property="og:description" content="@seo('description')" /> @endif
|
@if(seo('description')) <meta property="og:description" content="@seo('description')" /> @endif
|
||||||
@if(seo('image')) <meta property="og:image" content="@seo('image')" /> @endif
|
@if(seo('image')) <meta property="og:image" content="@seo('image')" /> @endif
|
||||||
|
@if(seo('url'))
|
||||||
|
<meta property="og:url" content="@seo('url')" />
|
||||||
|
<link rel="canonical" href="@seo('url')" />
|
||||||
|
@endif
|
||||||
|
|
||||||
@foreach(seo()->tags() as $tag)
|
@foreach(seo()->tags() as $tag)
|
||||||
{!! $tag !!}
|
{!! $tag !!}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Illuminate\Support\Str;
|
||||||
/**
|
/**
|
||||||
* @method $this title(string $title) Set the title.
|
* @method $this title(string $title) Set the title.
|
||||||
* @method $this description(string $description) Set the description.
|
* @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 site(string $site) Set the site name.
|
||||||
* @method $this image(string $url) Set the cover image.
|
* @method $this image(string $url) Set the cover image.
|
||||||
* @method $this twitter(enabled $bool = true) Enable the Twitter extension.
|
* @method $this twitter(enabled $bool = true) Enable the Twitter extension.
|
||||||
|
|
@ -51,7 +52,10 @@ class SEOManager
|
||||||
/** Get a list of used keys. */
|
/** Get a list of used keys. */
|
||||||
protected function getKeys(): array
|
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->defaults))
|
||||||
->merge(array_keys($this->values))
|
->merge(array_keys($this->values))
|
||||||
->unique()
|
->unique()
|
||||||
|
|
@ -172,6 +176,14 @@ class SEOManager
|
||||||
return $this->set('image', "https://s.useflipp.com/{$template}.png?s={$signature}&v={$query}");
|
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. */
|
/** Get all extra head tags. */
|
||||||
public function tags(): array
|
public function tags(): array
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -94,3 +94,27 @@ test('raw tags can be added to the template', function () {
|
||||||
|
|
||||||
expect(meta())->toContain('<meta foo bar>');
|
expect(meta())->toContain('<meta foo bar>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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('<meta property="og:url" content="http://localhost" />')
|
||||||
|
->toContain('<link rel="canonical" href="http://localhost" />');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('canonical url can be changed', function () {
|
||||||
|
seo()->withUrl();
|
||||||
|
|
||||||
|
seo()->url('http://foo.com/bar');
|
||||||
|
|
||||||
|
expect(meta())
|
||||||
|
->toContain('<meta property="og:url" content="http://foo.com/bar" />')
|
||||||
|
->toContain('<link rel="canonical" href="http://foo.com/bar" />');
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue