简体   繁体   English

在 Symfony2 中生成具有特定方案的绝对 URL

[英]Generate absolute URL with specific scheme in Symfony2

I want to generate an absolute URL with a specific scheme (https) in a Symfony2 controller.我想在 Symfony2 控制器中生成一个具有特定方案 (https) 的绝对 URL。 All the solutions I found point me to configure the targeted route so that it requires that scheme .我找到的所有解决方案都指向我配置目标路由,以便它需要该方案 But I need the route to remain accessible in http, so I can't set it to require https (in which case http requests are redirected to the corresponding https URL).但是我需要该路由在 http 中保持可访问性,所以我不能将它设置为需要 https(在这种情况下,http 请求被重定向到相应的 https URL)。

Is there a way to generate an URL with, in the scope of that URL generation only, a specific scheme?有没有办法生成一个 URL,在该 URL 生成的范围内,一个特定的方案?

I saw that using the 'network' keyword generates a "network path"-style URL, which looks like "//example.com/dir/file";我看到使用'network'关键字会生成一个“网络路径”样式的 URL,它看起来像“//example.com/dir/file”; so maybe I can simply do所以也许我可以简单地做

'https:' . $this->generateUrl($routeName, $parameters, 'network')

But I don't know if this will be robust enough to any route or request context.但我不知道这对于任何路由或请求上下文是否足够健壮。

UPDATE: after investigation in the URL generation code, this "network path" workaround seems fully robust.更新:在对 URL 生成代码进行调查后,这种“网络路径”解决方法似乎完全可靠。 A network path is generated exactly as an absolute URL, without a scheme before the "//".网络路径完全作为绝对 URL 生成,在“//”之前没有方案。

最好的方式去这个

$url = 'https:'.$this->generateUrl($routeName, $parameters, UrlGeneratorInterface::NETWORK_PATH)

According to the code or documentation, currently you cannot do that within the generateUrl method.根据代码或文档,目前您无法在 generateUrl 方法中执行此操作。 So your "hackish" solution is still the best, but as @RaymondNijland commented you are better off with str_replace :所以你的“hackish”解决方案仍然是最好的,但正如@RaymondNijland评论的那样,你最好使用str_replace

$url = str_replace('http:', 'https:', $this->generateUrl($routeName, $parameters));

If you want to make sure it's changed only at the beginning of the string, you can write:如果你想确保它只在字符串的开头改变,你可以写:

$url = preg_replace('/^http:/', 'https:', $this->generateUrl($routeName, $parameters));

No, the colon ( : ) has no special meaning in the regex so you don't have to escape it.不,冒号 ( : ) 在正则表达式中没有特殊含义,因此您不必转义它。

With default UrlGenerator , I don't think that is possible, if you don't want to mess with strings.使用默认的UrlGenerator如果您不想弄乱字符串,我认为这是不可能的。

You could make your own HttpsUrlGenerator extends UrlGenerator introducting one slight change:你可以让你自己的HttpsUrlGenerator extends UrlGenerator引入一个细微的变化:

Within method generate() , instead of:在方法generate() ,而不是:

return $this->doGenerate(
    $compiledRoute->getVariables(), 
    $route->getDefaults(), 
    $route->getRequirements(), 
    $compiledRoute->getTokens(), 
    $parameters, 
    $name, 
    $referenceType, 
    $compiledRoute->getHostTokens(), 
    $route->getSchemes() 
);

You could do:你可以这样做:

return $this->doGenerate(
    $compiledRoute->getVariables(), 
    $route->getDefaults(), 
    $route->getRequirements(), 
    $compiledRoute->getTokens(), 
    $parameters, 
    $name, 
    $referenceType, 
    $compiledRoute->getHostTokens(), 
    ['https']
);

As you can see, $route->getSchemes() gets pumped into doGenerate() based on the route settings (the tutorial link you provided above).如您所见, $route->getSchemes()根据路由设置(您在上面提供的教程链接$route->getSchemes()被注入doGenerate() ) 。

You could even go further and externalize this schema array and supply it via __construct .您甚至可以进一步将这个架构数组外部化并通过__construct提供它。

Hope this helps a bit ;)希望这个对你有帮助 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM