Reputation: 1893
Before user tries an unauthorized action I save:
1) Controller Name
2) Action
3) POST params
Then when user has a successful login I redirect it to...
$params["controller"] = "manage";
$params["action"] = $lastRequest["action"];
$params["name"] = "Ignacio";
$params["email"] = "[email protected]";
return $this->redirect()->toRoute("user-create", $params);
It does redirect, but NO posted params.
How can I emulate POST request on ZF2 from a controller? The point is I do not know where the user it is going to be redirected, so it could be GET or POST and any controller.
Upvotes: 2
Views: 11011
Reputation: 95
This solution might help but instead of using redirect, it forwards to another controller.
Before forwarding it to the other controller, replace (or update) the post params on the current request. The receiving controller then will see the replaced (or updated) post params.
// Controller that handles unauthorized access
class YourController extends AbstractActionController
{
// ...
$request = $this->getEvent()->getRequest();
$postParam = new \Zend\Stdlib\Parameters();
$postParam->set('controller', 'manage');
$postParam->set('action', $lastRequest['action']);
$postParam->set('name', 'your-name';
$postParam->set('email', 'your-email';
$request->setPost($postParam);
return $this->forward()->dispatch('Your\Other\UserCreateController', [
'action' => 'userCreate',
]);
// ...
}
Upvotes: 0
Reputation: 1893
This is the way I save a request and use it later to redirect to the correct action.
1) Unauthorized action saves the request with all GET/POST params.
$session = new Container('base');
$session->offsetSet("lastRequest", $event->getRequest());
2) After success login, redirect to requested
$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) Before controller action, retrieve all POST/GET params
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) Just use the request on any destination action as normal
class ManageController extends AbstractActionController {
public function createAction() {
if ($this->getRequest()->isPost()) {
$post = $this->getRequest()->getPost()->toArray();
}
}
Upvotes: 5
Reputation: 606
You can't redirect user with POST data, but ZF2 provide functionality to simulate this: http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-post-redirect-get-plugin
Upvotes: 2