在旧的ZF2应用程序中,如果匹配的路由以admin
开头,我会在调度侦听器中更改布局。现在我开始了一个新项目,并希望使用ZF3组件,但事件管理器确实有一些更改,我得到以下异常:
Uncaught TypeError:传递给Zend \ EventManager \ EventManager :: attach()的参数2必须是可调用的,没有给出
我不知道如何在ZF3中处理这个问题。以下是我在ZF2应用程序中更改布局的相关源代码:
Module.php
namespace Admin;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
class Module implements BootstrapListenerInterface {
public function onBootstrap(EventInterface $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$eventManager->attach($serviceManager->get('Admin\Listener\Dispatch'));
}
}
DispatchListener.php
namespace Admin\Listener;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
class DispatchListener extends AbstractListenerAggregate {
public function attach(EventManagerInterface $eventManager) {
$this->listeners[] = $eventManager->attach(
MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'), 100
);
}
public function onDispatch(EventInterface $event) {
$matchedRouteName = $event->getRouteMatch()->getMatchedRouteName();
if (strpos($matchedRouteName, 'admin') === 0) {
$event->getViewModel()->setTemplate('layout/admin');
}
}
}
答案 0 :(得分:1)
zf3更侧重于解耦组件,似乎聚合已被删除以附加事件请参阅api文档
简称附加消息说
def bgg_scrape_rank_page(browser, bgg_data):
time.sleep(1)
table = browser.find_element_by_xpath("//table[@class='collection_table']/tbody")
row = table.find_element_by_xpath("//tr[@id='row_']")
while row:
rank = row.find_element_by_xpath("//td[1]").text
game_name = row.find_element_by_xpath("//td[3]/div[2]/a").text
game_page = row.find_element_by_xpath("//td[3]/div[2]/a").get_attribute("href")
print rank, game_name, game_page
row = row.find_element_by_xpath("//following-sibling::tr")
我希望因为您没有指定eventName,所以您收到错误消息
更新
请参阅迁移指南的链接,从v2到v3,用于事件管理器
答案 1 :(得分:0)
在ZF3中,您可以通过以下简单方式更改控制器的布局:
<?php
namespace YourCompanyModule;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\MvcEvent;
class Module
{
// The "init" method is called on application start-up and
// allows to register an event listener.
public function init(ModuleManager $manager)
{
// Get event manager.
$eventManager = $manager->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method.
$sharedEventManager->attach(__NAMESPACE__, 'dispatch',
[$this, 'onDispatch'], 100);
}
// Event listener method.
public function onDispatch(MvcEvent $event)
{
// Get controller to which the HTTP request was dispatched.
$controller = $event->getTarget();
// Get fully qualified class name of the controller.
$controllerClass = get_class($controller);
// Get module name of the controller.
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
// Switch layout only for controllers belonging to our module.
if ($moduleNamespace == __NAMESPACE__) {
$viewModel = $event->getViewModel();
$viewModel->setTemplate('layout/layout2');
}
}
// ...
}