2016-11-08 139 views

回答

1

生成绝对网址应该包含端口。

use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 
... 

public function indexAction(Request $request) 
{ 
    $link = $this->generateUrl(
     'route_name', [ 
      'route'=>'params' 
     ], 
     UrlGeneratorInterface::ABSOLUTE_URL 
    ); 

    return $this->render('template', [ 
     'link' => $link; 
    ]); 
} 
+0

解决,谢谢。我试图自己构建网址,例如:'$ url =“www.example”。$ port。$ port。“/ someAction”'。生成绝对网址绝对是正确的方式。谢谢! –

2

Request对象拥有URI和端口。因此,从一个控制器内,你可以

public function indexAction(Request $request) 
{ 
    $uri = $request->getUri(); 
    $port = $request->getPort(); 
} 

如果你在一个控制器是不能确保在你的类注入RequestStack一则来自主请求URI获取和端口

$requestStack->getMasterRequest()->getUri(); 
+0

假设我在控制器'indexAction'中。从这里我想通过我的PHP模板传递一个指向另一个控制器的URL,例如'nextAction',包含当前用户端口号。 –