【问题标题】:Anchor in URL when using Symfony's redirect function使用 Symfony 的重定向功能时在 URL 中锚定
【发布时间】:2011-08-07 08:17:32
【问题描述】:

我正在使用

$this->redirect('route', array('id' => $id));

但我需要能够将“#”锚放在末尾,但我找不到这样做的方法。有什么想法吗?

代码

$this->redirect('route', array('id' => $id));

返回 /route/id/5 但我希望能够创建 /route/id/5#anchor7

【问题讨论】:

    标签: php symfony1


    【解决方案1】:
    $this->redirect($this->generateUrl('route', array('id' => $id)) . '#anchor7');
    

    更新:

    在 Symfony 3.2 中,您可以这样做:

    // generating a URL with a fragment (/settings#password)
    $this->redirectToRoute('user_settings', ['_fragment' => 'password']);
    

    https://symfony.com/blog/new-in-symfony-3-2-routing-improvements

    【讨论】:

    【解决方案2】:

    使用路由器:

    $router->generateUrl('awesome_route') . '#comment-2';
    

    Fabien Potencier 使用此方法:https://github.com/symfony/symfony/issues/3910#issuecomment-5110238

    【讨论】:

      【解决方案3】:

      类似但

      $this->redirect('@routename?id='.$id.'#anchor7');
      

      在 Symfony 1.4 中运行良好,解决方案似乎稍微简单

      【讨论】:

        猜你喜欢
        • 2017-05-12
        • 2017-07-04
        • 2020-06-04
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多