|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use App\Blog\Archive\ArchiveController; |
|
6
|
|
|
use App\Blog\BlogController; |
|
7
|
|
|
use App\Blog\Post\PostController; |
|
8
|
|
|
use App\Blog\Tag\TagController; |
|
9
|
|
|
use App\Controller\AuthController; |
|
10
|
|
|
use App\Controller\ContactController; |
|
11
|
|
|
use App\Controller\SiteController; |
|
12
|
|
|
use App\Controller\UserController; |
|
13
|
|
|
use Psr\Container\ContainerInterface; |
|
14
|
|
|
use Yiisoft\Http\Method; |
|
15
|
|
|
use Yiisoft\Router\FastRoute\UrlMatcher; |
|
16
|
|
|
use Yiisoft\Router\Group; |
|
17
|
|
|
use Yiisoft\Router\Route; |
|
18
|
|
|
use Yiisoft\Router\RouteCollection; |
|
19
|
|
|
use Yiisoft\Router\RouteCollectorInterface; |
|
20
|
|
|
use Yiisoft\Yii\Rest\RestGroup; |
|
21
|
|
|
|
|
22
|
|
|
class AppRouterFactory |
|
23
|
|
|
{ |
|
24
|
|
|
public function __invoke(ContainerInterface $container) |
|
25
|
|
|
{ |
|
26
|
|
|
$collector = $container->get(RouteCollectorInterface::class); |
|
27
|
|
|
$collector->addGroup(Group::create(null, $this->getRoutes())); |
|
28
|
|
|
$collector->addGroup(Group::create('/api', $this->getApiRoutes($container))); |
|
29
|
|
|
|
|
30
|
|
|
$routeCollection = new RouteCollection($collector); |
|
31
|
|
|
|
|
32
|
|
|
return new UrlMatcher($routeCollection); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getApiRoutes(ContainerInterface $container): array |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
RestGroup::create('/user', \App\Api\Controller\UserController::class, $container), |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function getRoutes(): array |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
// Lonely pages of site |
|
46
|
|
|
Route::get('/', [SiteController::class, 'index']) |
|
47
|
|
|
->name('site/index'), |
|
48
|
|
|
Route::methods([Method::GET, Method::POST], '/contact', [ContactController::class, 'contact']) |
|
49
|
|
|
->name('site/contact'), |
|
50
|
|
|
Route::methods([Method::GET, Method::POST], '/login', [AuthController::class, 'login']) |
|
51
|
|
|
->name('site/login'), |
|
52
|
|
|
Route::get('/logout', [AuthController::class, 'logout']) |
|
53
|
|
|
->name('site/logout'), |
|
54
|
|
|
|
|
55
|
|
|
// User |
|
56
|
|
|
Group::create('/user', [ |
|
57
|
|
|
// Index |
|
58
|
|
|
Route::get('[/page-{page:\d+}]', [UserController::class, 'index']) |
|
59
|
|
|
->name('user/index'), |
|
60
|
|
|
// Profile page |
|
61
|
|
|
Route::get('/{login}', [UserController::class, 'profile']) |
|
62
|
|
|
->name('user/profile'), |
|
63
|
|
|
]), |
|
64
|
|
|
|
|
65
|
|
|
// Blog routes |
|
66
|
|
|
Group::create('/blog', [ |
|
67
|
|
|
// Index |
|
68
|
|
|
Route::get('[/page{page:\d+}]', [BlogController::class, 'index']) |
|
69
|
|
|
->name('blog/index'), |
|
70
|
|
|
// Post page |
|
71
|
|
|
Route::get('/page/{slug}', [PostController::class, 'index']) |
|
72
|
|
|
->name('blog/post'), |
|
73
|
|
|
// Tag page |
|
74
|
|
|
Route::get('/tag/{label}[/page{page:\d+}]', [TagController::class, 'index']) |
|
75
|
|
|
->name('blog/tag'), |
|
76
|
|
|
// Archive |
|
77
|
|
|
Group::create('/blog', [ |
|
78
|
|
|
// Index page |
|
79
|
|
|
Route::get('', [ArchiveController::class, 'index']) |
|
80
|
|
|
->name('blog/archive/index'), |
|
81
|
|
|
// Yearly page |
|
82
|
|
|
Route::get('/{year:\d+}', [ArchiveController::class, 'yearlyArchive']) |
|
83
|
|
|
->name('blog/archive/year'), |
|
84
|
|
|
// Monthly page |
|
85
|
|
|
Route::get('/{year:\d+}-{month:\d+}[/page{page:\d+}]', [ArchiveController::class, 'monthlyArchive']) |
|
86
|
|
|
->name('blog/archive/month'), |
|
87
|
|
|
]), |
|
88
|
|
|
]), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|