-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Allow passing objects to the url helper #4178
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,22 +21,38 @@ class Url extends AbstractPlugin | |
/** | ||
* Generates a URL based on a route | ||
* | ||
* @param string $route RouteInterface name | ||
* @param array $params Parameters to use in url generation, if any | ||
* @param array|bool $options RouteInterface-specific options to use in url generation, if any. If boolean, and no fourth argument, used as $reuseMatchedParams | ||
* @param bool $reuseMatchedParams Whether to reuse matched parameters | ||
* @param string $route RouteInterface name | ||
* @param array|\Traversable $params Parameters to use in url generation, if any | ||
* @param array|bool $options RouteInterface-specific options to use in url generation, if any. | ||
* If boolean, and no fourth argument, used as $reuseMatchedParams. | ||
* @param bool $reuseMatchedParams Whether to reuse matched parameters | ||
* | ||
* @throws \Zend\Mvc\Exception\RuntimeException | ||
* @throws \Zend\Mvc\Exception\InvalidArgumentException | ||
* @throws \Zend\Mvc\Exception\DomainException | ||
* @return string | ||
* @throws Exception\DomainException if composed controller does not implement InjectApplicationEventInterface, or | ||
* router cannot be found in controller event | ||
* @throws Exception\RuntimeException if no RouteMatch instance or no matched route name present | ||
*/ | ||
public function fromRoute($route = null, array $params = array(), $options = array(), $reuseMatchedParams = false) | ||
public function fromRoute($route = null, $params = array(), $options = array(), $reuseMatchedParams = false) | ||
{ | ||
$controller = $this->getController(); | ||
if (!$controller instanceof InjectApplicationEventInterface) { | ||
throw new Exception\DomainException('Url plugin requires a controller that implements InjectApplicationEventInterface'); | ||
} | ||
|
||
if (! is_array($params)) { | ||
|
||
if (! $params instanceof \Traversable) { | ||
|
||
throw new Exception\InvalidArgumentException( | ||
'Params is expected to be an array of a Traversable object' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
|
||
} else { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why all the empty lines here at the beginning of each block? |
||
$params = iterator_to_array($params); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to be done inside an |
||
} | ||
|
||
$event = $controller->getEvent(); | ||
$router = null; | ||
$matches = null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ | |
use Zend\Mvc\ModuleRouteListener; | ||
use Zend\Mvc\Router\RouteMatch; | ||
use Zend\Mvc\Router\RouteStackInterface; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\View\Exception; | ||
use Zend\Stdlib\Exception as StdlibException; | ||
|
||
/** | ||
* Helper for making easy links and getting urls that depend on the routes and router. | ||
|
@@ -61,16 +63,17 @@ public function setRouteMatch(RouteMatch $routeMatch) | |
* Generates an url given the name of a route. | ||
* | ||
* @see Zend\Mvc\Router\RouteInterface::assemble() | ||
* @param string $name Name of the route | ||
* @param array $params Parameters for the link | ||
* @param array $options Options for the route | ||
* @param bool $reuseMatchedParams Whether to reuse matched parameters | ||
* @return string Url For the link href attribute | ||
* @throws Exception\RuntimeException If no RouteStackInterface was provided | ||
* @throws Exception\RuntimeException If no RouteMatch was provided | ||
* @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name | ||
* @param string $name Name of the route | ||
* @param array $params Parameters for the link | ||
* @param array|\Traversable $options Options for the route | ||
* @param bool $reuseMatchedParams Whether to reuse matched parameters | ||
* @return string Url For the link href attribute | ||
* @throws Exception\RuntimeException If no RouteStackInterface was provided | ||
* @throws Exception\RuntimeException If no RouteMatch was provided | ||
* @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name | ||
* @throws Exception\InvalidArgumentException If the params object was not an array or \Traversable object | ||
*/ | ||
public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false) | ||
public function __invoke($name = null, $params = array(), $options = array(), $reuseMatchedParams = false) | ||
{ | ||
if (null === $this->router) { | ||
throw new Exception\RuntimeException('No RouteStackInterface instance provided'); | ||
|
@@ -93,6 +96,20 @@ public function __invoke($name = null, array $params = array(), $options = array | |
} | ||
} | ||
|
||
if (! is_array($params)) { | ||
|
||
if (! $params instanceof \Traversable) { | ||
|
||
throw new Exception\InvalidArgumentException( | ||
'Params is expected to be an array of a Traversable object' | ||
); | ||
|
||
} else { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above. |
||
$params = iterator_to_array($params); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as noted for the controller plugin -- |
||
} | ||
|
||
if ($reuseMatchedParams && $this->routeMatch !== null) { | ||
$routeMatchParams = $this->routeMatch->getParams(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No space after
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Traversable;
in header.