zend-framework2 - ZF2 使用 POST 重定向

标签 zend-framework2

在用户尝试未经授权的操作之前,我保存:

1) Controller 名称

2) Action

3) POST 参数

然后当用户成功登录时,我将其重定向到...

$params["controller"] = "manage";
$params["action"] = $lastRequest["action"];
$params["name"] = "Ignacio";
$params["email"] = "ignacio@gmail.com";

return $this->redirect()->toRoute("user-create", $params);

它确实重定向,但没有发布参数。

如何从 Controller 模拟 ZF2 上的 POST 请求? 关键是我不知道用户将被重定向到哪里,所以它可能是 GET 或 POST 以及任何 Controller 。

最佳答案

这是我保存请求并稍后使用它重定向到正确操作的方式。

1) 未经授权的操作使用所有 GET/POST 参数保存请求。

$session = new Container('base');
$session->offsetSet("lastRequest", $event->getRequest());

2)登录成功后跳转到请求

$session = new Container('base');
if($lastRequest = $session->offsetGet("lastRequest")) {
    //Just redirect, because I could NOT find a way to POST params
    return $this->redirect()->toUrl($lastRequest->getRequestUri());
}

3) 在 Controller 操作之前,检索所有 POST/GET 参数

class Module {
//...
    public function init($moduleManager)
    {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 100);
    }

    public function preDispatch($event)
    {

    //Unauthorized request after success login
    $session = new Container('base');
    if($lastRequest = $session->offsetGet("lastRequest")) {
        $event->getTarget()->getRequest()->setMethod($lastRequest->getMethod());
        $event->getTarget()->getRequest()->setPost($lastRequest->getPost());
        $event->getTarget()->getRequest()->setQuery($lastRequest->getQuery());

        //Delete request
        $session->offsetSet("lastRequest", null);               
    }
}

4) 只需像往常一样对任何目标操作使用请求

class ManageController extends AbstractActionController {

    public function createAction() {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost()->toArray();
    }

}

关于zend-framework2 - ZF2 使用 POST 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867545/

相关文章:

zend-framework2 - Zend Framework 2 的 gitignore 文件有什么好的例子吗

zend-framework2 - Zend 2 - TableGateway Where 子句

zend-framework2 - 如何在zend框架二中创建服务层?

php - Zend framework 2 - 提交表单错误时使表单字段变粘

oauth - 用户的 Apigility 和 oAuth

php - Zend 框架 2 : Set options as "selected" in select list

session - ZF2中如何实现鉴权

php - 如何使用 Zend\Form\Factory 在 ZF2 字段集中设置过滤器和验证器?

zend-framework2 - 如何在 Zend Framework 2 中加载自定义库?

php - ZF2 LDAP SSL 证书不受信任