Skip to content

Commit 94fbb3b

Browse files
authored
Merge pull request #255 from brentmullen/fix/canonical
fix: add ability to use Url::current() as default canonical via config
2 parents b894d9e + 10711d2 commit 94fbb3b

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/SEOTools/SEOMeta.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,19 @@ public function getDescription()
470470
*/
471471
public function getCanonical()
472472
{
473+
if ($this->canonical) {
474+
return $this->canonical;
475+
}
476+
473477
$canonical_config = $this->config->get('defaults.canonical', false);
474478

475-
return $this->canonical ?: (($canonical_config === null) ? htmlspecialchars(app('url')->full()) : $canonical_config);
479+
if ($canonical_config === null || $canonical_config === 'full') {
480+
return htmlspecialchars(app('url')->full());
481+
} elseif ($canonical_config === 'current') {
482+
return htmlspecialchars(app('url')->current());
483+
}
484+
485+
return $canonical_config;
476486
}
477487

478488
/**

src/resources/config/seotools.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'description' => 'For those who helped create the Genki Dama', // set false to total remove
1515
'separator' => ' - ',
1616
'keywords' => [],
17-
'canonical' => false, // Set null for using Url::current(), set false to total remove
17+
'canonical' => false, // Set to null or 'full' to use Url::full(), set to 'current' to use Url::current(), set false to total remove
1818
'robots' => false, // Set to 'all', 'none' or any combination of index/noindex and follow/nofollow
1919
],
2020
/*

tests/SEOTools/SEOMetaTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,50 @@ public function test_set_canonical()
168168
$this->assertEquals($canonical, $this->seoMeta->getCanonical());
169169
}
170170

171+
public function dataTestUrls()
172+
{
173+
return [
174+
['http://localhost/hello/world', 'http://localhost/hello/world'],
175+
['http://localhost/hello/world?param=1', 'http://localhost/hello/world'],
176+
];
177+
}
178+
179+
/**
180+
* @dataProvider dataTestUrls
181+
*/
182+
public function test_get_canonical_null($fullUrl)
183+
{
184+
config()->set('defaults.canonical', null);
185+
$this->seoMeta = new SEOMeta(config());
186+
187+
$this->get($fullUrl);
188+
$this->assertEquals($fullUrl, $this->seoMeta->getCanonical());
189+
}
190+
191+
/**
192+
* @dataProvider dataTestUrls
193+
*/
194+
public function test_get_canonical_full($fullUrl)
195+
{
196+
config()->set('defaults.canonical', 'full');
197+
$this->seoMeta = new SEOMeta(config());
198+
199+
$this->get($fullUrl);
200+
$this->assertEquals($fullUrl, $this->seoMeta->getCanonical());
201+
}
202+
203+
/**
204+
* @dataProvider dataTestUrls
205+
*/
206+
public function test_get_canonical_current($fullUrl, $currentUrl)
207+
{
208+
config()->set('defaults.canonical', 'current');
209+
$this->seoMeta = new SEOMeta(config());
210+
211+
$this->get($fullUrl);
212+
$this->assertEquals($currentUrl, $this->seoMeta->getCanonical());
213+
}
214+
171215
public function test_set_amp()
172216
{
173217
$fullHeader = "<title>It's Over 9000!</title>";

0 commit comments

Comments
 (0)