Created
July 18, 2013 15:22
-
-
Save weierophinney/6030213 to your computer and use it in GitHub Desktop.
Module-conditional setup in ZF2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyFunnyValentine; | |
// Detect if the currently matched route belongs to this module. | |
// The assumption is that the controller name includes the module namespace. | |
class Module | |
{ | |
public function onBootstrap($e) | |
{ | |
$app = $e->getTarget(); | |
$events = $app->getEventManager(); | |
$events->attach('route', array($this, 'onRoute'), -100); | |
} | |
public function onRoute($e) | |
{ | |
$matches = $e->getRouteMatch(); | |
if (!$matches instanceof \Zend\Mvc\Router\RouteMatch) { | |
// No matches == not a match! | |
return; | |
} | |
$controller = $matches->getParam('controller'); | |
if (__NAMESPACE__ != substr($controller, 0, strlen(__NAMESPACE__))) { | |
// Namespace does not match! | |
return; | |
} | |
// Namespace matches! Do something interesting! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment