|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oscer\Cms\Frontend\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
|
6
|
|
|
use Illuminate\Support\Facades\Schema; |
|
7
|
|
|
use Oscer\Cms\Core\Http\Middleware\SetLocale; |
|
8
|
|
|
use Oscer\Cms\Core\Models\Option; |
|
9
|
|
|
use Oscer\Cms\Core\Models\Page; |
|
10
|
|
|
use Oscer\Cms\Frontend\Http\Controllers\PagesController; |
|
11
|
|
|
use Oscer\Cms\Frontend\Http\Controllers\PostsController; |
|
12
|
|
|
|
|
13
|
|
|
class FrontendRouter |
|
14
|
|
|
{ |
|
15
|
|
|
protected Router $router; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Router $router) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->router = $router; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function isMigrated() |
|
23
|
|
|
{ |
|
24
|
|
|
return Schema::hasTable('cms_posts') |
|
25
|
|
|
&& Schema::hasTable('cms_options'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function registerPagesRoutes(string $pathPrefix = '', $middleware = 'web') |
|
29
|
|
|
{ |
|
30
|
|
|
if (! $this->isMigrated()) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$pages = Page::query()->get('slug'); |
|
35
|
|
|
|
|
36
|
|
|
$this->router |
|
37
|
|
|
->middleware([$middleware, SetLocale::class]) |
|
38
|
|
|
->prefix($pathPrefix) |
|
39
|
|
|
->as('cms.') |
|
40
|
|
|
->group(function (Router $router) use ($pages) { |
|
41
|
|
|
if ($frontPageSlug = Option::getValueByKey('pages.front_page')) { |
|
42
|
|
|
$pages = $pages->filter(function (Page $page) use ($router, $frontPageSlug) { |
|
43
|
|
|
if ($page->slug === $frontPageSlug) { |
|
44
|
|
|
$router->get('/', [PagesController::class, 'frontPage']) |
|
45
|
|
|
->name('pages.front_page'); |
|
46
|
|
|
|
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return true; |
|
51
|
|
|
}); |
|
52
|
|
|
} |
|
53
|
|
|
$pages->each(function (Page $page) use ($router) { |
|
54
|
|
|
$router->get("/{$page->slug}", [PagesController::class, 'show']) |
|
55
|
|
|
->name("pages.{$page->slug}"); |
|
56
|
|
|
}); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function registerPostsRoutes(string $pathPrefix = 'posts', $middleware = 'web') |
|
61
|
|
|
{ |
|
62
|
|
|
$this->router |
|
63
|
|
|
->middleware([$middleware, SetLocale::class]) |
|
64
|
|
|
->prefix($pathPrefix) |
|
65
|
|
|
->as('cms.') |
|
66
|
|
|
->group(function (Router $router) { |
|
67
|
|
|
$router->get('/', [PostsController::class, 'index'])->name('posts.index'); |
|
68
|
|
|
$router->get('/{slug}', [PostsController::class, 'show'])->name('posts.show'); |
|
69
|
|
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|