这是我保存请求并稍后使用它来重定向到正确操作的方式。
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) 在控制器动作之前,检索所有 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();
}
}