Skip to content

Commit 5fe8825

Browse files
chillbramstancl
andauthored
Make universal routes work for controller middleware (#1151)
* Make universal routes work for controller middleware * add a fallback --------- Co-authored-by: chillbram <[email protected]> Co-authored-by: Samuel Štancl <[email protected]>
1 parent d268a06 commit 5fe8825

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Features/UniversalRoutes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function bootstrap(Tenancy $tenancy): void
3535

3636
public static function routeHasMiddleware(Route $route, $middleware): bool
3737
{
38-
if (in_array($middleware, $route->middleware(), true)) {
38+
if (in_array($middleware, $route->computedMiddleware ?? $route->middleware(), true)) {
3939
return true;
4040
}
4141

tests/UniversalRouteTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,46 @@ public function making_one_route_universal_doesnt_make_all_routes_universal()
6363
->assertSuccessful()
6464
->assertSee('acme');
6565
}
66+
67+
/** @test */
68+
public function universal_route_works_when_middleware_is_inserted_via_controller_middleware()
69+
{
70+
Route::middlewareGroup('universal', []);
71+
config(['tenancy.features' => [UniversalRoutes::class]]);
72+
73+
Route::get('/foo', [UniversalRouteController::class, 'show']);
74+
75+
$this->get('http://localhost/foo')
76+
->assertSuccessful()
77+
->assertSee('Tenancy is not initialized.');
78+
79+
$tenant = Tenant::create([
80+
'id' => 'acme',
81+
]);
82+
$tenant->domains()->create([
83+
'domain' => 'acme.localhost',
84+
]);
85+
86+
$this->get('http://acme.localhost/foo')
87+
->assertSuccessful()
88+
->assertSee('Tenancy is initialized.');
89+
}
90+
}
91+
92+
class UniversalRouteController
93+
{
94+
public function getMiddleware()
95+
{
96+
return array_map(fn($middleware) => [
97+
'middleware' => $middleware,
98+
'options' => [],
99+
], ['universal', InitializeTenancyByDomain::class]);
100+
}
101+
102+
public function show()
103+
{
104+
return tenancy()->initialized
105+
? 'Tenancy is initialized.'
106+
: 'Tenancy is not initialized.';
107+
}
66108
}

0 commit comments

Comments
 (0)