Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5153e55ff3
Fetching contributors…

Cannot retrieve contributors at this time

file 113 lines (96 sloc) 3.277 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\Controller\Plugin;

use Zend\Mvc\Exception\RuntimeException;
use Zend\Session\Container;
use Zend\Session\ManagerInterface as Manager;

/**
* Plugin to help facilitate Post/Redirect/Get (http://en.wikipedia.org/wiki/Post/Redirect/Get)
*
* @category Zend
* @package Zend_Mvc
* @subpackage Controller
*/
class PostRedirectGet extends AbstractPlugin
{
    /**
* @var Manager
*/
    protected $session;

    /**
* Set the session manager
*
* @param Manager $manager
* @return FlashMessenger
*/
    public function setSessionManager(Manager $manager)
    {
        $this->session = $manager;
        return $this;
    }

    /**
* Retrieve the session manager
*
* If none composed, lazy-loads a SessionManager instance
*
* @return Manager
*/
    public function getSessionManager()
    {
        if (!$this->session instanceof Manager) {
            $this->setSessionManager(Container::getDefaultManager());
        }
        return $this->session;
    }

    public function __invoke($redirect = null, $redirectToUrl = false)
    {
        $controller = $this->getController();
        $request = $controller->getRequest();
        $params = array();

        if (null === $redirect) {
            $routeMatch = $controller->getEvent()->getRouteMatch();

            $redirect = $routeMatch->getMatchedRouteName();
            $params = $routeMatch->getParams();
        }

        $container = new Container('prg_post1', $this->getSessionManager());

        if ($request->isPost()) {
            $container->setExpirationHops(1, 'post');
            $container->post = $request->getPost()->toArray();

            if (method_exists($controller, 'getPluginManager')) {
                // get the redirect plugin from the plugin manager
                $redirector = $controller->getPluginManager()->get('Redirect');
            } else {
                /*
* If the user wants to redirect to a route, the redirector has to come
* from the plugin manager -- otherwise no router will be injected
*/
                if ($redirectToUrl === false) {
                    throw new RuntimeException('Could not redirect to a route without a router');
                }

                $redirector = new Redirect();
            }

            if ($redirectToUrl === false) {
                $response = $redirector->toRoute($redirect, $params);
                $response->setStatusCode(303);
                return $response;
            }

            $response = $redirector->toUrl($redirect);
            $response->setStatusCode(303);

            return $response;
        } else {
            if ($container->post !== null) {
                $post = $container->post;
                unset($container->post);
                return $post;
            }

            return false;
        }
    }
}
Something went wrong with that request. Please try again.