From 7f3029bb3061a8402b79f676915a2d644ec116fb Mon Sep 17 00:00:00 2001 From: Ben Youngblood Date: Fri, 13 Jul 2012 13:01:54 -0700 Subject: [PATCH] Controller plugin that will help with PRG (see wikipedia link below) http://en.wikipedia.org/wiki/Post/Redirect/Get --- .../Mvc/Controller/Plugin/PostRedirectGet.php | 32 +++++++++++++++++++ library/Zend/Mvc/Controller/PluginManager.php | 14 ++++---- 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php diff --git a/library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php b/library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php new file mode 100644 index 00000000000..d0628d1b1fd --- /dev/null +++ b/library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php @@ -0,0 +1,32 @@ +getController(); + $request = $controller->getRequest(); + $flashMessenger = $controller->flashMessenger()->setNamespace('prg-post'); + + if ($request->isPost()) { + $flashMessenger->addMessage($request->getPost()->toArray()); + try { + return $controller->redirect()->toRoute($redirect); + } catch (RuntimeException $e) { + return $controller->redirect()->toUrl($redirect); + } + } else { + $messages = $flashMessenger->getMessages(); + if (count($messages)) { + return $messages[0]; + } else { + return false; + } + } + } +} + diff --git a/library/Zend/Mvc/Controller/PluginManager.php b/library/Zend/Mvc/Controller/PluginManager.php index 1642690ed9e..d46d7cccf20 100644 --- a/library/Zend/Mvc/Controller/PluginManager.php +++ b/library/Zend/Mvc/Controller/PluginManager.php @@ -33,12 +33,14 @@ class PluginManager extends AbstractPluginManager * @var array */ protected $invokableClasses = array( - 'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger', - 'forward' => 'Zend\Mvc\Controller\Plugin\Forward', - 'layout' => 'Zend\Mvc\Controller\Plugin\Layout', - 'params' => 'Zend\Mvc\Controller\Plugin\Params', - 'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect', - 'url' => 'Zend\Mvc\Controller\Plugin\Url', + 'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger', + 'forward' => 'Zend\Mvc\Controller\Plugin\Forward', + 'layout' => 'Zend\Mvc\Controller\Plugin\Layout', + 'params' => 'Zend\Mvc\Controller\Plugin\Params', + 'postredirectget' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet', + 'prg' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet', + 'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect', + 'url' => 'Zend\Mvc\Controller\Plugin\Url', ); /**