symfony从一个域重定向到另一个域

时间:2019-05-23 09:56:53

标签: symfony symfony4

我有一个Symfony 4.2网站,该网站在两个子域中提供内容:

a.example.com

b.example.com

如何在保留查询字符串的同时将对/resetting/reset/上对a的请求重定向到b

我尝试了以下操作,但是它什么也没做-即当我按下a.example.com/resetting/reset时,它就停留在那里。没有错误。

resetting:
    host: ^%a_domain%$
    path: ^/resetting/reset/
    controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
    defaults:
        path: '%b_domain%/resetting/reset/'
        permanent: true
        keepQueryParams: true

任何想法将不胜感激!

2 个答案:

答案 0 :(得分:0)

我无法使用yaml方法工作,因此我使用如下所示的自定义重定向操作覆盖了路由:

/**
 * @Route("/resetting/reset/{token}",
 *     name="fos_user_resetting_reset_override",
 *     host="{domain}",
 *     defaults={"domain"="%a_domain%"},
 *     requirements={"domain"="(%a_domain%)"},
 * )
 */
public function redirectResettingRouteToBDomain(string $token): RedirectResponse
{
    // generate the current url with an absolute path
    $path = $this->generateUrl(
        'fos_user_resetting_reset',
        ['token' => $token],
        UrlGeneratorInterface::ABSOLUTE_PATH
    );

    // replace A_DOMAIN with B_DOMAIN in the path
    $domain = preg_replace(
        '/' . preg_quote(getenv('A_DOMAIN'), '/') . '/',
        getenv('B_DOMAIN'),
        $this->getRequest()->server->get('HTTP_HOST')
    );

    // ensure the redirect works on http and https
    $url = '//' . $domain . $path;

    return $this->redirect($url);
}

请注意,我为A_DOMAINB_DOMAIN设置了环境变量,然后将它们用作a_domainb_domain参数。

本质上,上述控制器操作仅在A_DOMAIN上触发,覆盖fos_user_resetting_reset的默认B_DOMAIN路由,然后重定向请求。

答案 1 :(得分:0)

使用路由器类,如下更改主机:

$this->router->getContext()->setHost($host);
//$url = $this->router->generate('routeName');
//return $event->setResponse(new RedirectResponse($url));
return $this->redirectToRoute('routeName');
相关问题