diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index cdfb2c122428..56a116d405a2 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -811,6 +811,17 @@ public function setKeyResolver(callable $keyResolver) return $this; } + /** + * Clone a new instance of the URL generator with a different encryption key resolver. + * + * @param callable $keyResolver + * @return \Illuminate\Routing\UrlGenerator + */ + public function withKeyResolver(callable $keyResolver) + { + return (clone $this)->setKeyResolver($keyResolver); + } + /** * Get the root controller namespace. * diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index bb14513b7056..5c29954d5bae 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -867,6 +867,41 @@ public function testRouteGenerationWithBackedEnums() $this->assertSame('http://www.foo.com/foo/fruits', $url->route('foo.bar', CategoryBackedEnum::Fruits)); } + + public function testSignedUrlWithKeyResolver() + { + $url = new UrlGenerator( + $routes = new RouteCollection, + $request = Request::create('http://www.foo.com/') + ); + $url->setKeyResolver(function () { + return 'secret'; + }); + + $route = new Route(['GET'], 'foo', ['as' => 'foo', function () { + // + }]); + $routes->add($route); + + $request = Request::create($url->signedRoute('foo')); + + $this->assertTrue($url->hasValidSignature($request)); + + $request = Request::create($url->signedRoute('foo').'?tempered=true'); + + $this->assertFalse($url->hasValidSignature($request)); + + $url2 = $url->withKeyResolver(function () { + return 'other-secret'; + }); + + $this->assertFalse($url2->hasValidSignature($request)); + + $request = Request::create($url2->signedRoute('foo')); + + $this->assertTrue($url2->hasValidSignature($request)); + $this->assertFalse($url->hasValidSignature($request)); + } } class RoutableInterfaceStub implements UrlRoutable