繁体   English   中英

Zend重定向这两种方式之间的区别是什么

[英]Zend redirecting What's the difference between these 2 ways

之间有什么真正的区别吗?

$this->_redirect('controller/action');

$request->setControllerName('controller')
        ->setActionName('action');

我的猜测是第一个可能在幕后使用第二个。 有人知道吗?

不同之处在于重定向器帮助程序只发送HTTP重定向标头,而更改请求参数则需要运行调度程序 (如果已经运行则再次运行)以成功重定向,因此在何处调用该方法很重要。

似乎最大的区别是第一个发出至少两个HTTP请求(一个用于查找重定向,第二个用于执行它),另一个,在调度过程之前调用一个(如果我错了,请纠正我)。

重定向器帮助程序允许您使用重定向器对象来满足应用程序重定向到新URL的需要。 它提供了许多优于_redirect()方法的好处,例如能够将站点范围的行为预配置到重定向器对象中,或者使用内置的gotoSimple($ action,$ controller,$ module,$ params)接口,类似于Zend_Controller_Action :: _向前()。

与请求对象中的setController()和setAction()相比较的主要区别是您将更改url(302重定向),而不仅仅是操作。 此外,正如您所看到的,_redirect()方法是redirecotor帮助程序的快捷方式,它提供的功能比仅重定向更多。 你可以在这里看到: http//framework.zend.com/manual/en/zend.controller.actionhelpers.html#Redirector

$ this - > _ forward()方法与setController()和setAction()相同,是Zend_Controller_Action类的方法:

final protected function _forward($action, $controller = null, $module = null, array $params = null)
{
    $request = $this->getRequest();

    if (null !== $params) {
        $request->setParams($params);
    }

    if (null !== $controller) {
        $request->setControllerName($controller);

        // Module should only be reset if controller has been specified
        if (null !== $module) {
            $request->setModuleName($module);
        }
    }

    $request->setActionName($action)
            ->setDispatched(false);
}

如果您使用的是Zend_Controller_Action,则可以使用上面的方法,但是如果您使用的是Zend_Controller_Plugin,则需要直接使用请求对象。

例如,当您提交表单时,如果用户刷新页面,那么这是一个很好的实践重定向而不是转发,以防止表单被提交两次。

有关此过程的更多信息:

http://framework.zend.com/manual/en/zend.controller.dispatcher.html

http://devzone.zend.com/article/11978

第一个是通过发送302标头进行物理重定向。 第二个更类似于_forward()的作用 - 更改当前请求的控制器名称和操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM