Skip to content

Commit d630fb0

Browse files
fgnWidart
authored andcommitted
Resolved path problem for different locales (#21)
1 parent efc1f5c commit d630fb0

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

Http/Controllers/PublicController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(PageRepository $page, Application $app)
2828
*/
2929
public function uri($slug)
3030
{
31-
$page = $this->page->findBySlug($slug);
31+
$page = $this->page->findBySlugInLocale($slug, $this->locale);
3232

3333
$this->throw404IfNotFound($page);
3434

Repositories/Cache/CachePageDecorator.php

+16
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ function () {
4747
}
4848
);
4949
}
50+
51+
/**
52+
* @param $slug
53+
* @param $locale
54+
* @return object
55+
*/
56+
public function findBySlugInLocale($slug, $locale)
57+
{
58+
return $this->cache
59+
->tags($this->entityName, 'global')
60+
->remember("{$this->locale}.{$this->entityName}.findBySlugInLocale.{$slug}.{$locale}", $this->cacheTime,
61+
function () use ($slug, $locale) {
62+
return $this->repository->findBySlugInLocale($slug, $locale);
63+
}
64+
);
65+
}
5066
}

Repositories/Eloquent/EloquentPageRepository.php

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Modules\Page\Repositories\Eloquent;
22

3+
use Illuminate\Database\Eloquent\Builder;
34
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
45
use Modules\Page\Events\PageWasCreated;
56
use Modules\Page\Events\PageWasUpdated;
@@ -51,4 +52,21 @@ public function update($model, $data)
5152

5253
return $model;
5354
}
55+
56+
/**
57+
* @param $slug
58+
* @param $locale
59+
* @return object
60+
*/
61+
public function findBySlugInLocale($slug, $locale)
62+
{
63+
if (method_exists($this->model, 'translations')) {
64+
return $this->model->whereHas('translations', function (Builder $q) use ($slug, $locale) {
65+
$q->where('slug', $slug);
66+
$q->where('locale', $locale);
67+
})->with('translations')->first();
68+
}
69+
70+
return $this->model->where('slug', $slug)->where('locale', $locale)->first();
71+
}
5472
}

Repositories/PageRepository.php

+7
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public function findHomepage();
1515
* @return int
1616
*/
1717
public function countAll();
18+
19+
/**
20+
* @param $slug
21+
* @param $locale
22+
* @return object
23+
*/
24+
public function findBySlugInLocale($slug, $locale);
1825
}

0 commit comments

Comments
 (0)