Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[2.1] refresh() method in Redirect plugin #2100

Closed
wants to merge 1 commit into from

4 participants

fabicz Don't Add Me To Your Organization a.k.a The Travis Bot Maks3w Matthew Weier O'Phinney
fabicz

Common case of redirecting to same route after some processing.

Supports also toRoute(null) to use with PRG plugin.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 092fffd into a59928a).

Maks3w
Collaborator

@fabicz Are you talking about redirect from /foo to /foo? I really think that is a bad practice

fabicz

Why do you think so? I use that a lot. Mainly in PRG plugin scenario where I have same action to handle GET and POST.

Or pages where I split (big) form into few subpages on the same web address.

Matthew Weier O'Phinney

Setting milestone to 2.1, as this is a new method. Also: tests/Zend was moved to tests/ZendTest; please rebase.

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'redirect-refresh' of https://github.com/fabicz/zf2 into…
… feature/mvc-refresh

PR #2100

Conflicts:
	library/Zend/Mvc/Controller/Plugin/Redirect.php
	tests/ZendTest/Mvc/Controller/Plugin/RedirectTest.php
1db1531
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'redirect-refresh' of https://github.com/fabicz/zf2 into…
… feature/mvc-refresh

PR #2100

Conflicts:
	library/Zend/Mvc/Controller/Plugin/Redirect.php
	tests/ZendTest/Mvc/Controller/Plugin/RedirectTest.php
3b9d7b9
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'feature/mvc-refresh'
Close #2100
294ddfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 2, 2012
  1. refresh() method in Redirect plugin

    marek.nos authored
This page is out of date. Refresh to see the latest.
36 library/Zend/Mvc/Controller/Plugin/Redirect.php
View
@@ -29,6 +29,24 @@ class Redirect extends AbstractPlugin
protected $router;
/**
+ * Gets current route name
+ *
+ * @return string
+ */
+ protected function getCurrentRoute ()
+ {
+ return $this->getEvent()->getRouteMatch()->getMatchedRouteName();
+ }
+
+ /**
+ * Gets current route params
+ */
+ protected function getCurrentRouteParams()
+ {
+ return $this->getEvent()->getRouteMatch()->getParams();
+ }
+
+ /**
* Generates a URL based on a route
*
* @param string $route RouteInterface name
@@ -38,8 +56,14 @@ class Redirect extends AbstractPlugin
* @throws Exception\DomainException if composed controller does not implement InjectApplicationEventInterface, or
* router cannot be found in controller event
*/
- public function toRoute($route, array $params = array(), array $options = array())
+ public function toRoute($route = null, array $params = array(), array $options = array())
{
+ // Refresh if null provided
+ if (is_null($route)) {
+ $route = $this->getCurrentRoute();
+ $params = array_merge($this->getCurrentRouteParams(), $params);
+ }
+
$response = $this->getResponse();
$router = $this->getRouter();
@@ -65,6 +89,16 @@ public function toUrl($url)
}
/**
+ * Refresh to current route
+ *
+ * @return string
+ */
+ public function refresh()
+ {
+ return $this->toRoute($this->getCurrentRoute(), $this->getCurrentRouteParams());
+ }
+
+ /**
* Get the router
*
* @return RouteStackInterface
24 tests/Zend/Mvc/Controller/Plugin/RedirectTest.php
View
@@ -12,10 +12,12 @@
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Response;
+use Zend\Route\Request;
use Zend\Mvc\Controller\Plugin\Redirect as RedirectPlugin;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\Literal as LiteralRoute;
use Zend\Mvc\Router\SimpleRouteStack;
+use Zend\Mvc\Router\RouteMatch;
use ZendTest\Mvc\Controller\TestAsset\SampleController;
class RedirectTest extends TestCase
@@ -32,9 +34,13 @@ public function setUp()
),
)));
+ $routeMatch = new RouteMatch(array());
+ $routeMatch->setMatchedRouteName('home');
+
$event = new MvcEvent();
$event->setRouter($router);
$event->setResponse($this->response);
+ $event->setRouteMatch($routeMatch);
$this->controller = new SampleController();
$this->controller->setEvent($event);
@@ -95,4 +101,22 @@ public function testRedirectToRouteWithoutRouterInEventRaisesDomainException()
$this->setExpectedException('Zend\Mvc\Exception\DomainException', 'event compose a router');
$plugin->toRoute('home');
}
+
+ public function testPluginCanRefreshToRouteWhenProperlyConfigured()
+ {
+ $response = $this->plugin->refresh();
+ $this->assertTrue($response->isRedirect());
+ $headers = $response->getHeaders();
+ $location = $headers->get('Location');
+ $this->assertEquals('/', $location->getFieldValue());
+ }
+
+ public function testPluginCanRedirectToRouteWithNullWhenProperlyConfigured()
+ {
+ $response = $this->plugin->toRoute();
+ $this->assertTrue($response->isRedirect());
+ $headers = $response->getHeaders();
+ $location = $headers->get('Location');
+ $this->assertEquals('/', $location->getFieldValue());
+ }
}
Something went wrong with that request. Please try again.