I have a route called student, like this
'student' => array(
'type' => 'Hostname',
'options' => array(
'route' => ':subdomain.domain.com',
'constraints' => array(
'subdomain' => '([a-zA-Z0-9]*)'
...
And there are child routes in some of my modules. I need to run a function (e.g. checkSubdomain()) before execute any action of this child routes.
Anyone can help me?
Thank you guys! My code now:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkSubdomain'));
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function checkSubdomain(EventInterface $e) {
$app = $e->getApplication();
$sm = $app->getServiceManager();
$routeMatch = $e->getRouteMatch();
$matchedRouteName = $routeMatch->getMatchedRouteName();
$arr = explode('/',$matchedRouteName);
if ($arr[0]=='student') {
$subdomain = $routeMatch->getParam('subdomain');
$em = $sm->get('project_entitymanager');
$proj = $em->getRepository('Project\Entity\Project')->findOneBy(array('subdomain' => $subdomain));
if (!$proj) $e->stopPropagation();
}
}