From dd005fc041fbb850a6ed3dcf6d15dc28bfef8aa8 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 21 Jun 2022 21:20:18 +0300 Subject: [PATCH 1/3] Add RoutesBootloader --- app/routes/api.php | 19 ++++ app/routes/web.php | 20 +++++ app/src/App.php | 1 - .../Bootloader/LocaleSelectorBootloader.php | 24 ----- app/src/Bootloader/RoutesBootloader.php | 90 +++++++++++-------- 5 files changed, 91 insertions(+), 63 deletions(-) create mode 100644 app/routes/api.php create mode 100644 app/routes/web.php delete mode 100644 app/src/Bootloader/LocaleSelectorBootloader.php diff --git a/app/routes/api.php b/app/routes/api.php new file mode 100644 index 0000000..02af95f --- /dev/null +++ b/app/routes/api.php @@ -0,0 +1,19 @@ +add('html', '/.html')->controller(HomeController::class); +}; diff --git a/app/src/App.php b/app/src/App.php index 55c1cec..e4b5db9 100644 --- a/app/src/App.php +++ b/app/src/App.php @@ -106,7 +106,6 @@ class App extends Kernel * Application specific services and extensions. */ protected const APP = [ - Bootloader\LocaleSelectorBootloader::class, Bootloader\RoutesBootloader::class, // fast code prototyping diff --git a/app/src/Bootloader/LocaleSelectorBootloader.php b/app/src/Bootloader/LocaleSelectorBootloader.php deleted file mode 100644 index fd3530d..0000000 --- a/app/src/Bootloader/LocaleSelectorBootloader.php +++ /dev/null @@ -1,24 +0,0 @@ -addMiddleware(LocaleSelector::class); - } -} diff --git a/app/src/Bootloader/RoutesBootloader.php b/app/src/Bootloader/RoutesBootloader.php index 11c7cc5..c3f5297 100644 --- a/app/src/Bootloader/RoutesBootloader.php +++ b/app/src/Bootloader/RoutesBootloader.php @@ -1,52 +1,66 @@ setRoute( - name: 'html', - route: new Route('/.html', new Controller(HomeController::class)) - ); - - // fallback (default) route - $router->setDefault($this->defaultRoute()); + return [ + ErrorHandlerMiddleware::class, + JsonPayloadMiddleware::class, + HttpCollector::class, + LocaleSelector::class, + ]; } - /** - * Default route points to namespace of controllers. - */ - protected function defaultRoute(): RouteInterface + protected function middlewareGroups(): array { - // handle all /controller/action like urls - $route = new Route( - pattern: '/[[/]]', - target: new Namespaced('App\\Controller') - ); - - return $route->withDefaults([ - 'controller' => 'home', - 'action' => 'index', - ]); + return [ + 'web' => [ + CookiesMiddleware::class, + SessionMiddleware::class, + CsrfMiddleware::class, + ], + 'api' => [ + // + ] + ]; + } + + protected function defineRoutes(RoutingConfigurator $routes): void + { + $routes->import($this->dirs->get('app') . '/routes/web.php')->group('web'); + $routes->import($this->dirs->get('app') . '/routes/api.php')->group('api'); + + $routes->default('/[[/]]') + ->namespaced('App\\Controller') + ->defaults([ + 'controller' => 'home', + 'action' => 'index', + ]) + ->middleware([ + CookiesMiddleware::class, + SessionMiddleware::class, + CsrfMiddleware::class, + ]); } } From 51617b93a01b3a5dba09752134684be2d7db04ec Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 21 Jun 2022 21:22:59 +0300 Subject: [PATCH 2/3] Fix CS --- app/routes/api.php | 1 - app/src/Bootloader/RoutesBootloader.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/routes/api.php b/app/routes/api.php index 02af95f..951f566 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -15,5 +15,4 @@ | */ return function (RoutingConfigurator $routes) { - }; diff --git a/app/src/Bootloader/RoutesBootloader.php b/app/src/Bootloader/RoutesBootloader.php index c3f5297..289ba41 100644 --- a/app/src/Bootloader/RoutesBootloader.php +++ b/app/src/Bootloader/RoutesBootloader.php @@ -42,7 +42,7 @@ protected function middlewareGroups(): array ], 'api' => [ // - ] + ], ]; } From c3ad37ac396e3ce8caa544bdb4be34a1f43c704a Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 22 Jun 2022 10:41:31 +0300 Subject: [PATCH 3/3] Remove api routes, add examples with AuthTransportMiddleware --- app/routes/api.php | 18 ------------------ app/src/Bootloader/RoutesBootloader.php | 10 +++++----- 2 files changed, 5 insertions(+), 23 deletions(-) delete mode 100644 app/routes/api.php diff --git a/app/routes/api.php b/app/routes/api.php deleted file mode 100644 index 951f566..0000000 --- a/app/routes/api.php +++ /dev/null @@ -1,18 +0,0 @@ - 'cookie']) ], 'api' => [ - // + // new Autowire(AuthTransportMiddleware::class, ['transportName' => 'header']) ], ]; } @@ -49,7 +52,6 @@ protected function middlewareGroups(): array protected function defineRoutes(RoutingConfigurator $routes): void { $routes->import($this->dirs->get('app') . '/routes/web.php')->group('web'); - $routes->import($this->dirs->get('app') . '/routes/api.php')->group('api'); $routes->default('/[[/]]') ->namespaced('App\\Controller') @@ -58,9 +60,7 @@ protected function defineRoutes(RoutingConfigurator $routes): void 'action' => 'index', ]) ->middleware([ - CookiesMiddleware::class, - SessionMiddleware::class, - CsrfMiddleware::class, + // SomeMiddleware::class, ]); } }