diff --git a/CHANGELOG.md b/CHANGELOG.md index 068794caa..2c1c0227d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ for full details on how to migrate your v2 application. component. (That component also includes console-specific routes, which were removed from zend-router.) All service factories related to console functionality are also now provided by zend-mvc-console. +- [#104](https://github.com/zendframework/zend-mvc/pull/104) removes the `prg()` + plugin. It can now be installed separately via the + zendframework/zend-mvc-plugin-prg package. ### Fixed diff --git a/doc/book/migration/to-v3-0.md b/doc/book/migration/to-v3-0.md index 611016d84..2c4fd1e24 100644 --- a/doc/book/migration/to-v3-0.md +++ b/doc/book/migration/to-v3-0.md @@ -77,3 +77,24 @@ ensure console tooling continues to work. > recommend migrating your code to use standalone tools such as > [zf-console](https://github.com/zfcampus/zf-console) or > [Aura.Cli](https://github.com/auraphp/Aura.Cli). + +## Plugins + +The following plugins have been removed from the main zend-mvc repository, and +into their own standalone repositories. In all cases, please be sure to install +the [component installer as detailed above](#dependency-reduction) before +installing the plugins, to automate injection into your application +configuration. + +### prg() + +The `prg()` plugin is now provided via the +[zend-mvc-plugin-prg](https://github.com/zendframework/zend-mvc-plugin-prg) +component. + +```bash +$ composer require zendframework/zend-mvc-plugin-prg +``` + +`Zend\Mvc\Controller\Plugin\PostRedirectGet` becomes +`Zend\Mvc\Plugin\Prg\PostRedirectGet`. However, it is still mapped as `prg()`. diff --git a/src/Controller/Plugin/PostRedirectGet.php b/src/Controller/Plugin/PostRedirectGet.php deleted file mode 100644 index f503b57ce..000000000 --- a/src/Controller/Plugin/PostRedirectGet.php +++ /dev/null @@ -1,135 +0,0 @@ -getController(); - $request = $controller->getRequest(); - $container = $this->getSessionContainer(); - - if ($request->isPost()) { - $container->setExpirationHops(1, 'post'); - $container->post = $request->getPost()->toArray(); - return $this->redirect($redirect, $redirectToUrl); - } else { - if (null !== $container->post) { - $post = $container->post; - unset($container->post); - return $post; - } - - return false; - } - } - - /** - * @return Container - */ - public function getSessionContainer() - { - if (!isset($this->sessionContainer)) { - $this->sessionContainer = new Container('prg_post1'); - } - return $this->sessionContainer; - } - - /** - * @param Container $container - * @return PostRedirectGet - */ - public function setSessionContainer(Container $container) - { - $this->sessionContainer = $container; - return $this; - } - - /** - * TODO: Good candidate for traits method in PHP 5.4 with FilePostRedirectGet plugin - * - * @param string $redirect - * @param bool $redirectToUrl - * @return \Zend\Http\Response - * @throws \Zend\Mvc\Exception\RuntimeException - */ - protected function redirect($redirect, $redirectToUrl) - { - $controller = $this->getController(); - $params = []; - $options = ['query' => $controller->params()->fromQuery()]; - $reuseMatchedParams = false; - - if (null === $redirect) { - $routeMatch = $controller->getEvent()->getRouteMatch(); - - $redirect = $routeMatch->getMatchedRouteName(); - //null indicates to redirect for self. - $reuseMatchedParams = true; - } - - 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 (false === $redirectToUrl) { - throw new RuntimeException('Could not redirect to a route without a router'); - } - - $redirector = new Redirect(); - } - - if (false === $redirectToUrl) { - $response = $redirector->toRoute($redirect, $params, $options, $reuseMatchedParams); - $response->setStatusCode(303); - return $response; - } - - $response = $redirector->toUrl($redirect); - $response->setStatusCode(303); - - return $response; - } -} diff --git a/src/Controller/PluginManager.php b/src/Controller/PluginManager.php index d93d4edc0..8a3e4cea2 100644 --- a/src/Controller/PluginManager.php +++ b/src/Controller/PluginManager.php @@ -52,10 +52,6 @@ class PluginManager extends AbstractPluginManager 'layout' => Plugin\Layout::class, 'Params' => Plugin\Params::class, 'params' => Plugin\Params::class, - 'PostRedirectGet' => Plugin\PostRedirectGet::class, - 'postRedirectGet' => Plugin\PostRedirectGet::class, - 'postredirectget' => Plugin\PostRedirectGet::class, - 'prg' => Plugin\PostRedirectGet::class, 'Redirect' => Plugin\Redirect::class, 'redirect' => Plugin\Redirect::class, 'Url' => Plugin\Url::class, @@ -76,7 +72,6 @@ class PluginManager extends AbstractPluginManager Plugin\FlashMessenger::class => InvokableFactory::class, Plugin\Layout::class => InvokableFactory::class, Plugin\Params::class => InvokableFactory::class, - Plugin\PostRedirectGet::class => InvokableFactory::class, Plugin\Redirect::class => InvokableFactory::class, Plugin\Url::class => InvokableFactory::class, Plugin\CreateHttpNotFoundModel::class => InvokableFactory::class, @@ -90,7 +85,6 @@ class PluginManager extends AbstractPluginManager 'zendmvccontrollerpluginflashmessenger' => InvokableFactory::class, 'zendmvccontrollerpluginlayout' => InvokableFactory::class, 'zendmvccontrollerpluginparams' => InvokableFactory::class, - 'zendmvccontrollerpluginpostredirectget' => InvokableFactory::class, 'zendmvccontrollerpluginredirect' => InvokableFactory::class, 'zendmvccontrollerpluginurl' => InvokableFactory::class, 'zendmvccontrollerplugincreatehttpnotfoundmodel' => InvokableFactory::class, diff --git a/test/Controller/Plugin/PostRedirectGetTest.php b/test/Controller/Plugin/PostRedirectGetTest.php deleted file mode 100644 index 1fd72424a..000000000 --- a/test/Controller/Plugin/PostRedirectGetTest.php +++ /dev/null @@ -1,241 +0,0 @@ -addRoute('home', LiteralRoute::factory([ - 'route' => '/', - 'defaults' => [ - 'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController', - ] - ])); - - $router->addRoute('sub', SegmentRoute::factory([ - 'route' => '/foo/:param', - 'defaults' => [ - 'param' => 1 - ] - ])); - - $router->addRoute('ctl', SegmentRoute::factory([ - 'route' => '/ctl/:controller', - 'defaults' => [ - '__NAMESPACE__' => 'ZendTest\Mvc\Controller\TestAsset', - 'controller' => 'sample' - ] - ])); - - $this->controller = new SampleController(); - $this->request = new Request(); - $this->event = new MvcEvent(); - $this->routeMatch = new RouteMatch(['controller' => 'controller-sample', 'action' => 'postPage']); - - $this->event->setRequest($this->request); - $this->event->setRouteMatch($this->routeMatch); - $this->event->setRouter($router); - - $this->controller->setEvent($this->event); - } - - public function testReturnsFalseOnIntialGet() - { - $result = $this->controller->dispatch($this->request, $this->response); - $prgResult = $this->controller->prg('home'); - - $this->assertFalse($prgResult); - } - - public function testRedirectsToUrlOnPost() - { - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters([ - 'postval1' => 'value' - ])); - - $result = $this->controller->dispatch($this->request, $this->response); - $prgResultUrl = $this->controller->prg('/test/getPage', true); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultUrl); - $this->assertTrue($prgResultUrl->getHeaders()->has('Location')); - $this->assertEquals('/test/getPage', $prgResultUrl->getHeaders()->get('Location')->getUri()); - $this->assertEquals(303, $prgResultUrl->getStatusCode()); - } - - public function testRedirectsToRouteOnPost() - { - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters([ - 'postval1' => 'value1' - ])); - - $result = $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg('home'); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - } - - public function testReturnsPostOnRedirectGet() - { - $params = [ - 'postval1' => 'value1' - ]; - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters($params)); - - $result = $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg('home'); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - - // Do GET - $this->request = new Request(); - $this->controller->dispatch($this->request, $this->response); - $prgResult = $this->controller->prg('home'); - - $this->assertEquals($params, $prgResult); - - // Do GET again to make sure data is empty - $this->request = new Request(); - $this->controller->dispatch($this->request, $this->response); - $prgResult = $this->controller->prg('home'); - - $this->assertFalse($prgResult); - } - - /** - * @expectedException Zend\Router\Exception\RuntimeException - */ - public function testThrowsExceptionOnRouteWithoutRouter() - { - $controller = $this->controller; - $controller = $controller->getEvent()->setRouter(new SimpleRouteStack); - - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters([ - 'postval1' => 'value' - ])); - - $result = $this->controller->dispatch($this->request, $this->response); - $this->controller->prg('some/route'); - } - - public function testNullRouteUsesMatchedRouteName() - { - $this->controller->getEvent()->getRouteMatch()->setMatchedRouteName('home'); - - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters([ - 'postval1' => 'value1' - ])); - - $result = $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg(); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - } - - public function testReuseMatchedParameters() - { - $this->controller->getEvent()->getRouteMatch()->setMatchedRouteName('sub'); - - $this->request->setMethod('POST'); - $this->request->setPost(new Parameters([ - 'postval1' => 'value1' - ])); - - $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg(); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals('/foo/1', $prgResultRoute->getHeaders()->get('Location')->getUri()); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - } - - public function testReuseMatchedParametersWithSegmentController() - { - $expects = '/ctl/sample'; - $this->request->setMethod('POST'); - $this->request->setUri($expects); - $this->request->setPost(new Parameters([ - 'postval1' => 'value1' - ])); - - $routeMatch = $this->event->getRouter()->match($this->request); - $this->event->setRouteMatch($routeMatch); - - $moduleRouteListener = new ModuleRouteListener; - $moduleRouteListener->onRoute($this->event); - - $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg(); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals($expects, $prgResultRoute->getHeaders()->get('Location')->getUri(), 'expects to redirect for the same url'); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - } - - public function testKeepUrlQueryParameters() - { - $expects = '/ctl/sample'; - $this->request->setMethod('POST'); - $this->request->setUri($expects); - $this->request->setQuery(new Parameters([ - 'id' => '123', - ])); - - $routeMatch = $this->event->getRouter()->match($this->request); - $this->event->setRouteMatch($routeMatch); - - $moduleRouteListener = new ModuleRouteListener; - $moduleRouteListener->onRoute($this->event); - - $this->controller->dispatch($this->request, $this->response); - $prgResultRoute = $this->controller->prg(); - - $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); - $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); - $this->assertEquals($expects . '?id=123', $prgResultRoute->getHeaders()->get('Location')->getUri(), 'expects to redirect for the same url'); - $this->assertEquals(303, $prgResultRoute->getStatusCode()); - } -}