1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Page\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Modules\Core\Http\Controllers\BasePublicController; |
7
|
|
|
use Modules\Menu\Repositories\MenuItemRepository; |
8
|
|
|
use Modules\Page\Entities\Page; |
9
|
|
|
use Modules\Page\Repositories\PageRepository; |
10
|
|
|
|
11
|
|
|
class PublicController extends BasePublicController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var PageRepository |
15
|
|
|
*/ |
16
|
|
|
private $page; |
17
|
|
|
/** |
18
|
|
|
* @var Application |
19
|
|
|
*/ |
20
|
|
|
private $app; |
21
|
|
|
|
22
|
|
|
public function __construct(PageRepository $page, Application $app) |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
$this->page = $page; |
26
|
|
|
$this->app = $app; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $slug |
31
|
|
|
* @return \Illuminate\View\View |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function uri($slug) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$page = $this->findPageForSlug($slug); |
36
|
|
|
|
37
|
|
|
$this->throw404IfNotFound($page); |
38
|
|
|
|
39
|
|
|
$template = $this->getTemplateForPage($page); |
40
|
|
|
|
41
|
|
|
return view($template, compact('page')); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return \Illuminate\View\View |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function homepage() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$page = $this->page->findHomepage(); |
50
|
|
|
|
51
|
|
|
$this->throw404IfNotFound($page); |
52
|
|
|
|
53
|
|
|
$template = $this->getTemplateForPage($page); |
54
|
|
|
|
55
|
|
|
return view($template, compact('page')); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Find a page for the given slug. |
60
|
|
|
* The slug can be a 'composed' slug via the Menu |
61
|
|
|
* @param string $slug |
62
|
|
|
* @return Page |
63
|
|
|
*/ |
64
|
|
|
private function findPageForSlug($slug) |
65
|
|
|
{ |
66
|
|
|
$menuItem = app(MenuItemRepository::class)->findByUriInLanguage($slug, locale()); |
67
|
|
|
|
68
|
|
|
if ($menuItem) { |
69
|
|
|
return $this->page->find($menuItem->page_id); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->page->findBySlug($slug); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the template for the given page |
77
|
|
|
* or the default template if none found |
78
|
|
|
* @param $page |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function getTemplateForPage($page) |
82
|
|
|
{ |
83
|
|
|
return (view()->exists($page->template)) ? $page->template : 'default'; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Throw a 404 error page if the given page is not found |
88
|
|
|
* @param $page |
89
|
|
|
*/ |
90
|
|
|
private function throw404IfNotFound($page) |
91
|
|
|
{ |
92
|
|
|
if (is_null($page)) { |
93
|
|
|
$this->app->abort('404'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.