zf2重定向,查询字符串包含+而不是%20

时间:2017-01-09 20:46:50

标签: php redirect zend-framework zend-framework2

在ZF2中,我知道我可以使用如下附加的查询字符串创建301重定向:

$options = [ 
    'query' => [
        'string' => 'hello world', 
    ]
];

return $this->redirect()
            ->toRoute('myRoute', [], $options)
            ->setStatusCode(301);

但是,这会重定向到附加了hello%20world的网址。在ZF2中,有没有办法编写此重定向,并附加hello+world

2 个答案:

答案 0 :(得分:2)

由于ZF2不使用urlencode而不是rawurlencode提供使用查询字符串重定向的本机函数,因此我们编写了一个重定向的自定义方法。不是很漂亮,但现在解决了我们的问题:

private function redirectToPageFive($query)
{   
    $location = ($_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://')
              . $_SERVER['HTTP_HOST']
              . '/search?query='
              . urlencode($query)
              . '&page=5';

    header("Location: $location", true, 301);
    exit;
}

答案 1 :(得分:1)

您的网址正常url_encode d。它和#34;转变了#34;放入URL的有效字符。

在接收端,您的"hello%20world"将自动收到"hello world"

无需修复,代码按预期工作。

相关问题