2014-09-04 54 views
1

行,所以我有一个控制器的动作和2个与之相关联的路线:如何更改树枝模板路由生成中使用的主机?

/** 
* @Route("/index/preview/", name="mybundle.preview_index") 
* @Route("/", name="mybundle.index") 
* @Template 
*/ 
public function indexAction(Request $request) 
{ 
    $preview = ($request->get('_route') === 'mybundle.preview_index'); 
    $host = $request->getHttpHost(); //domain.com 
    if(!$preivew){ 
     $host = 'domain2.com'; 
    } 
    return array(
     'preivew' => $preview, 
     'host' => $host, 
     'basePath' => $preview?'mybundle.preview_':'mybundle.', 
    ); 
} 

然后我想生成取决于主机上的树枝模板内的路线:

{{ path(basePath~'index') }} 
//Then somehow pass the host to this so that i get the intended domain 

如果我被访问使用预览路线这条路线的话,我会得到:

domain.com/index/preview/ 

但如果我不是它会给我:

domain2.com/ 

我曾尝试

  • 从控制器内设置路由器环境,但在树枝产生,这并不改变路线

回答

2

我想通了。而不是使用path()的我必须使用url()并设置主机在路由器的情况下:

if(!$preview){ 
    $context = $this->get('router')->getContext(); 
    $context->setHost($host); 
} 

然后树枝:

{{ url(basePath~'index') }}