diff --git a/library/Zend/Mvc/Controller/Plugin/Redirect.php b/library/Zend/Mvc/Controller/Plugin/Redirect.php index 3930f1128ef..97976e31ba6 100644 --- a/library/Zend/Mvc/Controller/Plugin/Redirect.php +++ b/library/Zend/Mvc/Controller/Plugin/Redirect.php @@ -28,6 +28,24 @@ class Redirect extends AbstractPlugin protected $response; 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 * @@ -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(); @@ -64,6 +88,16 @@ public function toUrl($url) return $response; } + /** + * Refresh to current route + * + * @return string + */ + public function refresh() + { + return $this->toRoute($this->getCurrentRoute(), $this->getCurrentRouteParams()); + } + /** * Get the router * diff --git a/tests/Zend/Mvc/Controller/Plugin/RedirectTest.php b/tests/Zend/Mvc/Controller/Plugin/RedirectTest.php index 732728afefb..c2546ecab59 100644 --- a/tests/Zend/Mvc/Controller/Plugin/RedirectTest.php +++ b/tests/Zend/Mvc/Controller/Plugin/RedirectTest.php @@ -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()); + } }