Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created July 18, 2013 15:22
Show Gist options
  • Save weierophinney/6030213 to your computer and use it in GitHub Desktop.
Save weierophinney/6030213 to your computer and use it in GitHub Desktop.
Module-conditional setup in ZF2
<?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