Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow null value for route name in PRG plugin #2242

Merged
merged 2 commits into from

2 participants

@juriansluiman

In a lot of PRG patterns the POST request happens to the same url as the GET request. This PR keeps the flexibility to set the route name but it also defaults to the currently matched route name.

Previously I did:

$route = $this->getEvent()->getRouteMatch()->getMatchedRouteName();

if (($result = $this->prg($route)) instanceof Response) {
    return $result;
}  elseif (false !== $result) {
    // Do stuff
}

Where now you're able to:

if (($result = $this->prg()) instanceof Response) {
    return $result;
}  elseif (false !== $result) {
    // Do stuff
}

Test is made to assert the behaviour.

juriansluiman added some commits
@juriansluiman juriansluiman When redirect string is null, use current route
In a lot of PRG patterns the POST request happens to the same url
as the GET request. It keeps the flexibility to set the route name
but it also defaults to the current matched route name.
9c7181a
@juriansluiman juriansluiman Create test to assert null redirect value is allowed 9799715
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2242] Updated README
- Detailed the null route
38ac5e1
@weierophinney weierophinney merged commit 9799715 into zendframework:master
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2242] Updated README
- Detailed the null route
e455e27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2012
  1. @juriansluiman

    When redirect string is null, use current route

    juriansluiman authored
    In a lot of PRG patterns the POST request happens to the same url
    as the GET request. It keeps the flexibility to set the route name
    but it also defaults to the current matched route name.
  2. @juriansluiman
This page is out of date. Refresh to see the latest.
View
8 library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php
@@ -23,10 +23,14 @@
*/
class PostRedirectGet extends AbstractPlugin
{
- public function __invoke($redirect, $redirectToUrl = false)
+ public function __invoke($redirect = null, $redirectToUrl = false)
{
$controller = $this->getController();
- $request = $controller->getRequest();
+ $request = $controller->getRequest();
+
+ if (null === $redirect) {
+ $redirect = $controller->getEvent()->getRouteMatch()->getMatchedRouteName();
+ }
$container = new Container('prg_post1');
View
19 tests/ZendTest/Mvc/Controller/Plugin/PostRedirectGetTest.php
@@ -116,4 +116,23 @@ public function testThrowsExceptionOnRouteWithoutRouter()
$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(array(
+ '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());
+ }
}
Something went wrong with that request. Please try again.