Skip to content

Commit dad0b72

Browse files
authored
Merge pull request #14 from saydum/master
2 parents 34fa82f + 05f7935 commit dad0b72

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

src/Casts/UrlCast.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public function get($model, string $key, $value, array $attributes)
1515

1616
public function set($model, string $key, $value, array $attributes)
1717
{
18-
return parse_url((string)$value, PHP_URL_PATH) ?? '/';
18+
$parsedUrl = parse_url((string)$value);
19+
20+
$path = isset($parsedUrl['path']) ? '/' . ltrim($parsedUrl['path'], '/') : '/';
21+
$query = isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '';
22+
23+
return $path . $query;
1924
}
2025
}

src/Rules/UrlRule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class UrlRule implements InvokableRule
1010
{
1111
public function __invoke($attribute, $value, $fail)
1212
{
13-
if (! is_string(parse_url($value, PHP_URL_PATH))) {
13+
if (! is_string($value) || !preg_match('/^\/[^\s]*$/', $value)) {
1414
$fail('The :attribute must be a valid url path.');
1515
}
1616
}

src/SeoManager.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,26 @@ private function custom(): array
4242

4343
public function url(string $url = null): Stringable
4444
{
45-
return str($url ?? request()->path())
45+
return str($url ?? request()->getRequestUri())
4646
->trim('/')
4747
->prepend('/');
4848
}
4949

5050
public function getCacheKey(string $url = null): string
5151
{
52-
return (string) $this->url($url)->slug();
52+
$url = (string) $this->url($url);
53+
$parsedUrl = parse_url($url);
54+
$path = $parsedUrl["path"] ?? '/';
55+
$query = isset($parsedUrl["query"]) ? $this->normalizeQuery($parsedUrl["query"]) : '';
56+
57+
return md5($path . '?' . $query);
58+
}
59+
60+
protected function normalizeQuery(string $query): string
61+
{
62+
parse_str($query, $params);
63+
ksort($params);
64+
return http_build_query($params);
5365
}
5466

5567
public function meta(): SeoMeta
@@ -85,13 +97,22 @@ public function flushCache(string $key = null): void
8597

8698
public function byUrl(): Model|Seo|null
8799
{
88-
if(isset($this->persisted[(string) $this->url()])) {
89-
return $this->persisted[(string) $this->url()];
100+
$url = (string)$this->url();
101+
102+
$seo = Seo::query()->where("url", $url)->first();
103+
104+
if ($seo) {
105+
return $seo;
106+
}
107+
108+
$baseUrl = parse_url($url, PHP_URL_PATH);
109+
$seo = Seo::query()->where("url", $baseUrl)->first();
110+
111+
if ($seo) {
112+
return $seo;
90113
}
91114

92-
return Seo::query()
93-
->where('url', (string) $this->url())
94-
->first();
115+
return null;
95116
}
96117

97118
public function __toString(): string

tests/SeoFeaturesTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@ public function it_basic_usage(): void
4040
$this->assertEquals('Description', seo()->meta()->description());
4141
}
4242

43+
/**
44+
* @test
45+
* @return void
46+
*/
47+
public function it_basic_usage_with_params(): void
48+
{
49+
$this->app->instance('request', Request::create('/test?page=2'));
50+
51+
$seo = SeoModel::query()->create([
52+
'url' => '/test?page=2',
53+
'title' => 'Title',
54+
'description' => 'Description params',
55+
]);
56+
57+
$this->assertEquals($seo->title, seo()->meta()->title());
58+
59+
$seo->update([
60+
'title' => 'New title params',
61+
]);
62+
63+
$this->assertEquals($seo->title, seo()->meta()->title());
64+
65+
seo()->title('Custom title params');
66+
67+
$this->assertEquals('Custom title params', seo()->meta()->title());
68+
$this->assertEquals('Description params', seo()->meta()->description());
69+
}
4370

4471
/**
4572
* @test

0 commit comments

Comments
 (0)