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
12 use Zend\EventManager\AbstractListenerAggregate
;
13 use Zend\EventManager\EventManagerInterface
;
15 class RouteListener
extends AbstractListenerAggregate
18 * Attach to an event manager
20 * @param EventManagerInterface $events
21 * @param int $priority
24 public function attach(EventManagerInterface
$events, $priority = 1)
26 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_ROUTE
, [$this, 'onRoute']);
30 * Listen to the "route" event and attempt to route the request
32 * If no matches are returned, triggers "dispatch.error" in order to
33 * create a 404 response.
35 * Seeds the event with the route match on completion.
38 * @return null|Router\RouteMatch
40 public function onRoute($e)
42 $target = $e->getTarget();
43 $request = $e->getRequest();
44 $router = $e->getRouter();
45 $routeMatch = $router->match($request);
47 if (!$routeMatch instanceof Router\RouteMatch
) {
48 $e->setName(MvcEvent
::EVENT_DISPATCH_ERROR
);
49 $e->setError(Application
::ERROR_ROUTER_NO_MATCH
);
51 $results = $target->getEventManager()->triggerEvent($e);
52 if (count($results)) {
53 return $results->last();
56 return $e->getParams();
59 $e->setRouteMatch($routeMatch);