Skip to content

Commit

Permalink
Controller plugin that will help with PRG (see wikipedia link below)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjyoungblood committed Jul 13, 2012
1 parent 95011ba commit 7f3029b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
32 changes: 32 additions & 0 deletions library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Zend\Mvc\Controller\Plugin;

use Zend\Mvc\Router\Exception\RuntimeException;

class PostRedirectGet extends AbstractPlugin
{
public function __invoke($redirect)
{
$controller = $this->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;
}
}
}
}

14 changes: 8 additions & 6 deletions library/Zend/Mvc/Controller/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

/**
Expand Down

0 comments on commit 7f3029b

Please sign in to comment.