Skip to content

Commit

Permalink
Merge pull request #26 from edersoares/middleware
Browse files Browse the repository at this point in the history
Adds middleware to a route
  • Loading branch information
edersoares authored Dec 4, 2024
2 parents 0382284 + 051f498 commit 602da0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Frontier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private static function proxy(array $config): void
$methods = [];
$replaces = [];
$rewrite = [];
$middleware = [];
$cache = false;
$proxyAll = true;

Expand All @@ -69,6 +70,12 @@ private static function proxy(array $config): void
$methods = array_map(fn ($method) => strtoupper($method), $methods);
}

if (str_starts_with($segment, 'middleware(') && str_ends_with($segment, ')')) {
$replace = substr($segment, 11, -1);

$middleware[] = $replace;
}

if (str_starts_with($segment, 'replace(') && str_ends_with($segment, ')')) {
$replace = substr($segment, 8, -1);

Expand Down Expand Up @@ -98,6 +105,7 @@ private static function proxy(array $config): void
$uri = str_replace('//', '/', $uri);

Route::match($methods, $uri, FrontendProxyController::class)
->middleware($middleware)
->where('uri', '.*')
->setDefaults([
'uri' => $uri,
Expand Down
10 changes: 10 additions & 0 deletions tests/FrontendProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'/web',
'/all-methods::methods(get,head,options,post,put,patch,delete)',
'/with-cache::cache',
'/middleware::middleware(auth)',
],
]));

Expand Down Expand Up @@ -189,3 +190,12 @@
// Remove cache
$this->artisan('view:clear');
});

test('proxy passing by middleware', function () {
Http::fake([
'frontier.test/*',
]);

$this->getJson('/middleware')
->assertStatus(401);
});

0 comments on commit 602da0c

Please sign in to comment.