3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller\Plugin
;
13 use Zend\EventManager\EventInterface
;
14 use Zend\Mvc\Exception
;
15 use Zend\Mvc\InjectApplicationEventInterface
;
16 use Zend\Mvc\ModuleRouteListener
;
17 use Zend\Mvc\MvcEvent
;
18 use Zend\Mvc\Router\RouteStackInterface
;
20 class Url
extends AbstractPlugin
23 * Generates a URL based on a route
25 * @param string $route RouteInterface name
26 * @param array|Traversable $params Parameters to use in url generation, if any
27 * @param array|bool $options RouteInterface-specific options to use in url generation, if any.
28 * If boolean, and no fourth argument, used as $reuseMatchedParams.
29 * @param bool $reuseMatchedParams Whether to reuse matched parameters
31 * @throws \Zend\Mvc\Exception\RuntimeException
32 * @throws \Zend\Mvc\Exception\InvalidArgumentException
33 * @throws \Zend\Mvc\Exception\DomainException
36 public function fromRoute($route = null, $params = [], $options = [], $reuseMatchedParams = false)
38 $controller = $this->getController();
39 if (!$controller instanceof InjectApplicationEventInterface
) {
40 throw new Exception\
DomainException('Url plugin requires a controller that implements InjectApplicationEventInterface');
43 if (!is_array($params)) {
44 if (!$params instanceof Traversable
) {
45 throw new Exception\
InvalidArgumentException(
46 'Params is expected to be an array or a Traversable object'
49 $params = iterator_to_array($params);
52 $event = $controller->getEvent();
55 if ($event instanceof MvcEvent
) {
56 $router = $event->getRouter();
57 $matches = $event->getRouteMatch();
58 } elseif ($event instanceof EventInterface
) {
59 $router = $event->getParam('router', false);
60 $matches = $event->getParam('route-match', false);
62 if (!$router instanceof RouteStackInterface
) {
63 throw new Exception\
DomainException('Url plugin requires that controller event compose a router; none found');
66 if (3 == func_num_args() && is_bool($options)) {
67 $reuseMatchedParams = $options;
71 if ($route === null) {
73 throw new Exception\
RuntimeException('No RouteMatch instance present');
76 $route = $matches->getMatchedRouteName();
78 if ($route === null) {
79 throw new Exception\
RuntimeException('RouteMatch does not contain a matched route name');
83 if ($reuseMatchedParams && $matches) {
84 $routeMatchParams = $matches->getParams();
86 if (isset($routeMatchParams[ModuleRouteListener
::ORIGINAL_CONTROLLER
])) {
87 $routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener
::ORIGINAL_CONTROLLER
];
88 unset($routeMatchParams[ModuleRouteListener
::ORIGINAL_CONTROLLER
]);
91 if (isset($routeMatchParams[ModuleRouteListener
::MODULE_NAMESPACE
])) {
92 unset($routeMatchParams[ModuleRouteListener
::MODULE_NAMESPACE
]);
95 $params = array_merge($routeMatchParams, $params);
98 $options['name'] = $route;
99 return $router->assemble($params, $options);