标头位置时,ZF2重定向()不起作用

时间:2013-09-07 11:21:52

标签: php redirect zend-framework2

我有一个带有索引操作的控制器,我使用重定向来路由:

$this->redirect()->toRoute('dummy-route')->setStatusCode('301');

这适用于我的本地开发机器,但是一旦我将其部署到暂存,重定向就不再起作用了。 我设置了一个标题('Location:'),只是为了查看重定向是否正常工作,它确实按预期工作。

什么可以使zf2重定向在第二台机器上不起作用?

@ noobie-php是的,这是代码:

public function indexAction()
{
    $this->redirect()->toRoute('base-calculator')->setStatusCode('301');
    exit;
}

路线:

'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
                    'base-calculator' => array(
                            'type'    => 'Segment',
                            'options' => array(
                                    'route'    => '/calculate[/:criteriaFirst][/:criteriaSecond]',
                                    'constraints' => array(
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ),
                                    'defaults' => array(
                                            'controller' => 'Application\Controller\Index',
                                            'action'     => 'calculator',
                                    ),
                            ),
                    )

1 个答案:

答案 0 :(得分:11)

你面前有return陈述吗?

return $this->redirect()->to route('foo/bar');

/编辑:好的,现在我知道你没有,这里有解释。

该框架有一个称为短路的概念。这意味着无论何时在路由或调度期间返回响应,此响应都将立即发送给客户端。这样做不需要渲染视图脚本等,以加快速度。

Redirect插件创建一个Response对象,其中设置了状态代码(301或302)并注入了Location头。此位置标头包含您要重定向到的新网址。

如果未在此调用前放置return,则会创建一个Response对象,但不会再返回短路。它只是隐藏在框架内的某个地方。根据您自己的业务逻辑,您有时可能会被重定向,但大多数情况下都没有。

因此,与the manual also points out一样,您必须使用return来实现此短路

相关问题