Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refresh() method in Redirect plugin

  • Loading branch information...
commit 092fffdb2b8c985a5100582db405a2fdac14dd67 1 parent 84abe1c
marek.nos authored
View
36 library/Zend/Mvc/Controller/Plugin/Redirect.php
@@ -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
View
24 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());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.