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\ApiInfo; |
10
|
|
|
use App\Controller\ApiUserController; |
11
|
|
|
use App\Controller\AuthController; |
12
|
|
|
use App\Controller\ContactController; |
13
|
|
|
use App\Controller\SiteController; |
14
|
|
|
use App\Controller\UserController; |
15
|
|
|
use App\Middleware\ApiDataWrapper; |
16
|
|
|
use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponse; |
17
|
|
|
use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsJson; |
18
|
|
|
use Yiisoft\Yii\Web\Data\Middleware\FormatDataResponseAsXml; |
19
|
|
|
use Yiisoft\Yii\Web\Data\DataResponse; |
20
|
|
|
use Psr\Container\ContainerInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
23
|
|
|
use Yiisoft\Http\Method; |
24
|
|
|
use Yiisoft\Router\FastRoute\UrlMatcher; |
25
|
|
|
use Yiisoft\Router\Group; |
26
|
|
|
use Yiisoft\Router\Route; |
27
|
|
|
use Yiisoft\Router\RouteCollection; |
28
|
|
|
use Yiisoft\Router\RouteCollectorInterface; |
29
|
|
|
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface; |
30
|
|
|
|
31
|
|
|
class AppRouterFactory |
32
|
|
|
{ |
33
|
|
|
public function __invoke(ContainerInterface $container) |
34
|
|
|
{ |
35
|
|
|
$routes = [ |
36
|
|
|
// Lonely pages of site |
37
|
|
|
Route::get('/', [SiteController::class, 'index']) |
38
|
|
|
->name('site/index'), |
39
|
|
|
Route::methods([Method::GET, Method::POST], '/contact', [ContactController::class, 'contact']) |
40
|
|
|
->name('site/contact'), |
41
|
|
|
Route::methods([Method::GET, Method::POST], '/login', [AuthController::class, 'login']) |
42
|
|
|
->name('site/login'), |
43
|
|
|
Route::get('/logout', [AuthController::class, 'logout']) |
44
|
|
|
->name('site/logout'), |
45
|
|
|
|
46
|
|
|
// User |
47
|
|
|
Group::create('/user', [ |
48
|
|
|
// Index |
49
|
|
|
Route::get('[/page-{page:\d+}]', [UserController::class, 'index']) |
50
|
|
|
->name('user/index'), |
51
|
|
|
// Profile page |
52
|
|
|
Route::get('/{login}', [UserController::class, 'profile']) |
53
|
|
|
->name('user/profile'), |
54
|
|
|
]), |
55
|
|
|
|
56
|
|
|
// User |
57
|
|
|
Group::create('/api', [ |
58
|
|
|
Route::get('/info/v1', function (DataResponseFactoryInterface $responseFactory) { |
59
|
|
|
return $responseFactory->createResponse(['version' => '1.0', 'author' => 'yiisoft']); |
60
|
|
|
})->name('api/info/v1'), |
61
|
|
|
Route::get('/info/v2', ApiInfo::class) |
62
|
|
|
->addMiddleware(FormatDataResponseAsJson::class) |
63
|
|
|
->name('api/info/v2'), |
64
|
|
|
Route::get('/user', [ApiUserController::class, 'index']) |
65
|
|
|
->name('api/user/index'), |
66
|
|
|
Route::get('/user/{login}', [ApiUserController::class, 'profile']) |
67
|
|
|
->addMiddleware(FormatDataResponseAsJson::class) |
68
|
|
|
->name('api/user/profile'), |
69
|
|
|
], $container)->addMiddleware(ApiDataWrapper::class)->addMiddleware(FormatDataResponseAsXml::class), |
70
|
|
|
|
71
|
|
|
// Blog routes |
72
|
|
|
Group::create('/blog', [ |
73
|
|
|
// Index |
74
|
|
|
Route::get('[/page{page:\d+}]', [BlogController::class, 'index']) |
75
|
|
|
->name('blog/index'), |
76
|
|
|
// Post page |
77
|
|
|
Route::get('/page/{slug}', [PostController::class, 'index']) |
78
|
|
|
->name('blog/post'), |
79
|
|
|
// Tag page |
80
|
|
|
Route::get('/tag/{label}[/page{page:\d+}]', [TagController::class, 'index']) |
81
|
|
|
->name('blog/tag'), |
82
|
|
|
// Archive |
83
|
|
|
Group::create('/blog', [ |
84
|
|
|
// Index page |
85
|
|
|
Route::get('', [ArchiveController::class, 'index']) |
86
|
|
|
->name('blog/archive/index'), |
87
|
|
|
// Yearly page |
88
|
|
|
Route::get('/{year:\d+}', [ArchiveController::class, 'yearlyArchive']) |
89
|
|
|
->name('blog/archive/year'), |
90
|
|
|
// Monthly page |
91
|
|
|
Route::get('/{year:\d+}-{month:\d+}[/page{page:\d+}]', [ArchiveController::class, 'monthlyArchive']) |
92
|
|
|
->name('blog/archive/month') |
93
|
|
|
]), |
94
|
|
|
]), |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$collector = $container->get(RouteCollectorInterface::class); |
98
|
|
|
$collector->addGroup( |
99
|
|
|
Group::create(null, $routes) |
100
|
|
|
->addMiddleware(FormatDataResponse::class) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return new UrlMatcher(new RouteCollection($collector)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|