我有一条名为student的路线,就像这样
'student' => array(
'type' => 'Hostname',
'options' => array(
'route' => ':subdomain.domain.com',
'constraints' => array(
'subdomain' => '([a-zA-Z0-9]*)'
...
我的一些模块中有子路径。我需要在执行此子路由的任何操作之前运行一个函数(例如checkSubdomain())。
任何人都可以帮助我?
谢谢你们!我的代码现在:
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();
}
}
答案 0 :(得分:1)
根据“检查”中实际需要完成的操作,我会将事件监听器附加到MvcEvent::EVENT_ROUTE
事件或MvcEvent::EVENT_DISPATCH
例如
// Module.php
public function onBootstrap(MvcEvent $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'checkSubdomain'));
}
public function checkSubdomain(EventInterface $event) {
// Use $event to fetch the required event criteria
// and 'check' sub domain here
}
答案 1 :(得分:0)
您可以在Module
班级init
功能中执行此操作。
在module/Application/Module.php
和init