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 ModuleRouteListener
extends AbstractListenerAggregate
17 const MODULE_NAMESPACE
= '__NAMESPACE__';
18 const ORIGINAL_CONTROLLER
= '__CONTROLLER__';
21 * Attach to an event manager
23 * @param EventManagerInterface $events
24 * @param int $priority
26 public function attach(EventManagerInterface
$events, $priority = 1)
28 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_ROUTE
, [$this, 'onRoute'], $priority);
32 * Listen to the "route" event and determine if the module namespace should
33 * be prepended to the controller name.
35 * If the route match contains a parameter key matching the MODULE_NAMESPACE
36 * constant, that value will be prepended, with a namespace separator, to
37 * the matched controller parameter.
42 public function onRoute(MvcEvent
$e)
44 $matches = $e->getRouteMatch();
45 if (!$matches instanceof Router\RouteMatch
) {
46 // Can't do anything without a route match
50 $module = $matches->getParam(self
::MODULE_NAMESPACE
, false);
52 // No module namespace found; nothing to do
56 $controller = $matches->getParam('controller', false);
58 // no controller matched, nothing to do
62 // Ensure the module namespace has not already been applied
63 if (0 === strpos($controller, $module)) {
67 // Keep the originally matched controller name around
68 $matches->setParam(self
::ORIGINAL_CONTROLLER
, $controller);
70 // Prepend the controllername with the module, and replace it in the
72 $controller = $module . '\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $controller)));
73 $matches->setParam('controller', $controller);