Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function setPrefix(string $prefix): void
* @param string $prefix
* @param callable $cb
* @return Router
* @throws
* @throws RouterException
*/
public function prefix(string $prefix, callable $cb): Router
{
Expand All @@ -195,17 +195,18 @@ public function prefix(string $prefix, callable $cb): Router
/**
* Add a domain constraint for a group of routes
*
* @param string $domainPattern
* @param string $domain_pattern
* @param callable $cb
* @return Router
* @throws RouterException
*/
public function domain(string $domainPattern): Router
public function domain(string $domain_pattern, callable $cb): Router
{
$previousDomain = $this->domain;
$this->domain = $domain_pattern;

$this->domain = $domainPattern;
call_user_func_array($cb, [$this]);

$this->domain = $previousDomain;
$this->domain = null;

return $this;
}
Expand Down Expand Up @@ -252,6 +253,10 @@ public function route(array $definition): void
$route->middleware($definition['middleware']);
}

if (isset($definition['domain'])) {
$route->withDomain($definition['domain']);
}

$route->where($where);
}

Expand Down
52 changes: 52 additions & 0 deletions tests/Routing/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,56 @@ public function test_angle_bracket_param_with_wildcard_in_domain()
$this->assertTrue($route->match('/foo', 'app.api.example.com'));
$this->assertEquals('app', $route->getParameter('sub'));
}

public function test_router_route_method_with_domain_definition()
{
$router = \Bow\Router\Router::getInstance();

$router->route([
'path' => '/api/domain-test',
'method' => 'GET',
'handler' => fn() => 'domain route',
'domain' => 'api.example.com'
]);

$routes = $router->getRoutes();
$route = end($routes['GET']);

$this->assertTrue($route->match('/api/domain-test', 'api.example.com'));
$this->assertFalse($route->match('/api/domain-test', 'other.example.com'));
}

public function test_router_route_method_with_wildcard_domain()
{
$router = \Bow\Router\Router::getInstance();

$router->route([
'path' => '/api/wildcard-domain',
'method' => 'GET',
'handler' => fn() => 'wildcard domain',
'domain' => '*.example.com'
]);

$routes = $router->getRoutes();
$route = end($routes['GET']);

$this->assertTrue($route->match('/api/wildcard-domain', 'api.example.com'));
$this->assertTrue($route->match('/api/wildcard-domain', 'www.example.com'));
$this->assertFalse($route->match('/api/wildcard-domain', 'example.com'));
}

public function test_router_domain_group_method()
{
$router = \Bow\Router\Router::getInstance();

$router->domain('admin.example.com', function ($router) {
$router->get('/admin/dashboard', fn() => 'admin dashboard');
});

$routes = $router->getRoutes();
$route = end($routes['GET']);

$this->assertTrue($route->match('/admin/dashboard', 'admin.example.com'));
$this->assertFalse($route->match('/admin/dashboard', 'other.example.com'));
}
}
Loading