Skip to content

Commit 75a0753

Browse files
authored
Fix OPTIONS method bug when use same path but diff domain (#35714)
1 parent 0be33de commit 75a0753

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Illuminate/Routing/CompiledRouteCollection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ public function getRoutesByMethod()
252252
})
253253
->map(function (Collection $routes) {
254254
return $routes->mapWithKeys(function (Route $route) {
255+
if ($domain = $route->getDomain()) {
256+
return [$domain.'/'.$route->uri => $route];
257+
}
258+
255259
return [$route->uri => $route];
256260
})->all();
257261
})

tests/Integration/Routing/CompiledRouteCollectionTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,59 @@ public function testTrailingSlashIsTrimmedWhenMatchingCachedRoutes()
490490
$this->assertSame('foo', $this->collection()->match($request)->getName());
491491
}
492492

493+
public function testRouteWithSamePathAndSameMethodButDiffDomainNameWithOptionsMethod()
494+
{
495+
$routes = [
496+
'foo_domain' => $this->newRoute('GET', 'same/path', [
497+
'uses' => 'FooController@index',
498+
'as' => 'foo',
499+
'domain' => 'foo.localhost',
500+
]),
501+
'bar_domain' => $this->newRoute('GET', 'same/path', [
502+
'uses' => 'BarController@index',
503+
'as' => 'bar',
504+
'domain' => 'bar.localhost',
505+
]),
506+
'no_domain' => $this->newRoute('GET', 'same/path', [
507+
'uses' => 'BarController@index',
508+
'as' => 'no_domain',
509+
]),
510+
];
511+
512+
$this->routeCollection->add($routes['foo_domain']);
513+
$this->routeCollection->add($routes['bar_domain']);
514+
$this->routeCollection->add($routes['no_domain']);
515+
516+
$expectedMethods = [
517+
'OPTIONS',
518+
];
519+
520+
$this->assertSame($expectedMethods, $this->collection()->match(
521+
Request::create('http://foo.localhost/same/path', 'OPTIONS')
522+
)->methods);
523+
524+
$this->assertSame($expectedMethods, $this->collection()->match(
525+
Request::create('http://bar.localhost/same/path', 'OPTIONS')
526+
)->methods);
527+
528+
$this->assertSame($expectedMethods, $this->collection()->match(
529+
Request::create('http://no.localhost/same/path', 'OPTIONS')
530+
)->methods);
531+
532+
$this->assertEquals([
533+
'HEAD' => [
534+
'foo.localhost/same/path' => $routes['foo_domain'],
535+
'bar.localhost/same/path' => $routes['bar_domain'],
536+
'same/path' => $routes['no_domain'],
537+
],
538+
'GET' => [
539+
'foo.localhost/same/path' => $routes['foo_domain'],
540+
'bar.localhost/same/path' => $routes['bar_domain'],
541+
'same/path' => $routes['no_domain'],
542+
],
543+
], $this->collection()->getRoutesByMethod());
544+
}
545+
493546
/**
494547
* Create a new Route object.
495548
*

0 commit comments

Comments
 (0)