diff --git a/Extractor/ExposedRoutesExtractor.php b/Extractor/ExposedRoutesExtractor.php index 88903206..dfb927a7 100644 --- a/Extractor/ExposedRoutesExtractor.php +++ b/Extractor/ExposedRoutesExtractor.php @@ -11,6 +11,7 @@ namespace FOS\JsRoutingBundle\Extractor; +use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouterInterface; use JMS\I18nRoutingBundle\Router\I18nLoader; @@ -58,6 +59,7 @@ public function __construct(RouterInterface $router, array $routesToExpose = arr public function getRoutes() { $exposedRoutes = array(); + /** @var $route Route */ foreach ($this->getExposedRoutes() as $name => $route) { // Maybe there is a better way to do that... $compiledRoute = $route->compile(); @@ -66,10 +68,12 @@ public function getRoutes() array_fill_keys($compiledRoute->getVariables(), null) ); $requirements = $route->getRequirements(); + $host = method_exists($route, 'getHost') ? $route->getHost() : ''; $exposedRoutes[$name] = new ExtractedRoute( $compiledRoute->getTokens(), $defaults, - $requirements + $requirements, + $host ); } diff --git a/Extractor/ExtractedRoute.php b/Extractor/ExtractedRoute.php index 23032dce..9e3f37a4 100644 --- a/Extractor/ExtractedRoute.php +++ b/Extractor/ExtractedRoute.php @@ -16,12 +16,14 @@ class ExtractedRoute private $tokens; private $defaults; private $requirements; + private $host; - public function __construct(array $tokens, array $defaults, array $requirements) + public function __construct(array $tokens, array $defaults, array $requirements, $host = '') { $this->tokens = $tokens; $this->defaults = $defaults; $this->requirements = $requirements; + $this->host = $host; } public function getTokens() @@ -38,4 +40,9 @@ public function getRequirements() { return $this->requirements; } + + public function getHost() + { + return $this->host; + } } diff --git a/Resources/js/router.js b/Resources/js/router.js index 084bf2b5..29e4149d 100644 --- a/Resources/js/router.js +++ b/Resources/js/router.js @@ -206,7 +206,9 @@ fos.Router.prototype.generate = function(name, opt_params, absolute) { url = this.context_.base_url + url; if (goog.object.containsKey(route.requirements, "_scheme") && this.getScheme() != route.requirements["_scheme"]) { - url = route.requirements["_scheme"] + "://" + this.getHost() + url; + url = route.requirements["_scheme"] + "://" + (route.host || this.getHost()) + url; + } else if (route.host && this.getHost() !== route.host) { + url = this.getScheme() + "://" + route.host + url; } else if (absolute === true) { url = this.getScheme() + "://" + this.getHost() + url; } diff --git a/Resources/js/router.test.js b/Resources/js/router.test.js index 951fcf4f..12cf1b8b 100644 --- a/Resources/js/router.test.js +++ b/Resources/js/router.test.js @@ -48,6 +48,45 @@ function testGenerateUsesSchemeRequirements() { assertEquals('https://localhost/foo/bar', router.generate('homepage')); } +function testGenerateUsesHost() { + var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, { + homepage: { + tokens: [['text', '/bar']], + defaults: {}, + requirements: {}, + host: 'otherhost' + } + }); + + assertEquals('http://otherhost/foo/bar', router.generate('homepage')); +} + +function testGenerateUsesHostWhenTheSameSchemeRequirementGiven() { + var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, { + homepage: { + tokens: [['text', '/bar']], + defaults: {}, + requirements: {"_scheme": "http"}, + host: 'otherhost' + } + }); + + assertEquals('http://otherhost/foo/bar', router.generate('homepage')); +} + +function testGenerateUsesHostWhenAnotherSchemeRequirementGiven() { + var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, { + homepage: { + tokens: [['text', '/bar']], + defaults: {}, + requirements: {"_scheme": "https"}, + host: 'otherhost' + } + }); + + assertEquals('https://otherhost/foo/bar', router.generate('homepage')); +} + function testGenerateUsesAbsoluteUrl() { var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, { homepage: { diff --git a/Resources/public/js/router.js b/Resources/public/js/router.js index ecfd37c8..1024b4a5 100644 --- a/Resources/public/js/router.js +++ b/Resources/public/js/router.js @@ -8,5 +8,5 @@ function p(a){if(a.e!=a.a.length){for(var c=0,b=0;cgetSerializer(), $this->getExtractor(array( 'literal' => new ExtractedRoute(array(array('text', '/homepage')), array(), array()), - 'blog' => new ExtractedRoute(array(array('variable', '/', '[^/]+?', 'slug'), array('text', '/blog-post')), array(), array()), + 'blog' => new ExtractedRoute(array(array('variable', '/', '[^/]+?', 'slug'), array('text', '/blog-post')), array(), array(), 'localhost'), )) ); $response = $controller->indexAction($this->getRequest('/'), 'json'); - $this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[]},"blog":{"tokens":[["variable","\/","[^\/]+?","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[]}},"prefix":"","host":"","scheme":""}', $response->getContent()); + $this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"host":""},"blog":{"tokens":[["variable","\/","[^\/]+?","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"host":"localhost"}},"prefix":"","host":"","scheme":""}', $response->getContent()); } public function testGenerateWithCallback()