From 55538e8b8365609f71d8736cb1514648c0cbec50 Mon Sep 17 00:00:00 2001 From: dewanakl Date: Sun, 25 Feb 2024 17:30:17 +0700 Subject: [PATCH] feat: change route in service provider --- app/Kernel.php | 1 - app/Middleware/CorsMiddleware.php | 4 ++-- app/Middleware/GzipMiddleware.php | 6 +++--- app/Providers/RouteServiceProvider.php | 8 +++++++- composer.json | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Kernel.php b/app/Kernel.php index 697d3e9..decc0d4 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -59,7 +59,6 @@ public function middlewares(): array \App\Middleware\CorsMiddleware::class, \App\Middleware\XSSMiddleware::class, \App\Middleware\GzipMiddleware::class, - \App\Middleware\CsrfMiddleware::class ]; } diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php index 911e691..4ab0cae 100644 --- a/app/Middleware/CorsMiddleware.php +++ b/app/Middleware/CorsMiddleware.php @@ -18,7 +18,7 @@ public function handle(Request $request, Closure $next) $header = respond()->getHeader(); $header->set('Access-Control-Allow-Origin', '*'); - $vary = (!$header->has('Vary')) ? [] : explode(', ', $header->get('Vary')); + $vary = $header->has('Vary') ? explode(', ', $header->get('Vary')) : []; $vary = array_unique([...$vary, 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin', 'User-Agent']); $header->set('Vary', join(', ', $vary)); @@ -39,7 +39,7 @@ public function handle(Request $request, Closure $next) $header->set( 'Access-Control-Allow-Headers', - $request->server->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'Accept, Authorization, Content-Type, Origin, Token, User-Agent') + $request->server->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'Accept, Authorization, Content-Type, Origin, User-Agent') ); return respond()->setCode(Respond::HTTP_NO_CONTENT); diff --git a/app/Middleware/GzipMiddleware.php b/app/Middleware/GzipMiddleware.php index 9446dcd..4c01773 100644 --- a/app/Middleware/GzipMiddleware.php +++ b/app/Middleware/GzipMiddleware.php @@ -28,11 +28,11 @@ public function handle(Request $request, Closure $next): Stream|Respond return $response; } - if (!str_contains($request->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) { + if (!str_contains($request->server->get('HTTP_ACCEPT_ENCODING', ''), 'gzip')) { return $response; } - $compressed = gzencode($response->getContent(false), 1); + $compressed = gzencode($response->getContent(false), 3); if ($compressed === false) { return $response; @@ -40,7 +40,7 @@ public function handle(Request $request, Closure $next): Stream|Respond $response->setContent($compressed); - $vary = (!$response->headers->has('Vary')) ? [] : explode(', ', $response->headers->get('Vary')); + $vary = $response->headers->has('Vary') ? explode(', ', $response->headers->get('Vary')) : []; $vary = array_unique([...$vary, 'Accept-Encoding']); $response->headers diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 18bf9ec..ac38b65 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Middleware\CsrfMiddleware; use Core\Facades\Provider; use Core\Routing\Route; use Core\Routing\Router; @@ -16,6 +17,11 @@ class RouteServiceProvider extends Provider public function booting() { $this->app->singleton(Router::class); - Route::setRouteFromCacheIfExist(); + + if (!Route::setRouteFromCacheIfExist()) { + Route::middleware(CsrfMiddleware::class)->group(function () { + Route::setRouteFromFile(); + }); + } } } diff --git a/composer.json b/composer.json index 3621141..dc528b0 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "license": "MIT", "require": { "php": "^8.1", - "kamu/framework": "^3.2" + "kamu/framework": "^3.3" }, "require-dev": { "fakerphp/faker": "^1.23", @@ -35,4 +35,4 @@ }, "minimum-stability": "stable", "prefer-stable": true -} \ No newline at end of file +}