Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.