'Cake PHP 2 event listener not firing
I'm working on a Cake PHP 2 project that contains a custom module feature allowing for custom plugins. The functionality for this to work is loaded correctly, and the module I'm working on called QueueManager contains a controller linked to a button which dispatches an event, I'm then listening for this event in a file called app/modules/QueueManagerModule/Lib/Event/DispatchJobListener.php and it's here where I'm simply trying to write to a file to check if the listener is listening.
I'm not getting any errors logged to my error.log file, what am I missing?
My controller contains the following to dispatch the event:
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEvent', 'Event');
class QueueController extends AppController
{
/**
* Define the name
*/
public $name = 'Queue';
/**
* Define the name
*/
public $layout = 'QueueManagerLayout';
/**
* Loaded models
*/
public $uses = array(
'QueueManagerJob',
'QueueManagerFailedJob'
);
/**
* View: Queue/dashboard
*/
public function dashboard()
{
if ($this->request->is('post')) {
$this->getEventManager()->dispatch(new CakeEvent(
'QueueManagerModule.QueueManagerJob.dispatch',
$this, array('payload' => ['name' => 'john'])
));
}
}
}
My Lib/Event/DispatchJobListener.php is listening for the event:
<?php
App::uses('CakeEventListener', 'Event');
class DispatchJobListener implements CakeEventListener {
public function implementedEvents() {
return array(
'QueueManagerModule.QueueManagerJob.dispatch' => 'test'
);
}
public function test($event) {
fopen("456465564.txt", "w");
}
}
And I've loaded the events in my module's Config/events.php file:
<?php
App::uses('CakeEventManager', 'Event');
App::uses('DispatchJobListener', 'Lib/Event');
CakeEventManager::instance()->attach(new DispatchJobListener());
Why don't I get any file created?
My custom module is loaded and contains an App::build section which loads the modules dircetories, and it's here where the Config is loaded, I've stubbed the events.php file in the project's main Config folder, and have loaded it through the bootstrap.php
<?php
App::uses('BaseModule', 'Modules');
App::uses('CakeEventManager', 'Event');
/**
* Helper class to load modules of a specific format from /app/modules directory,
* and create instances that can connect to system events, modify system behaviours etc.
*
* Usage:
*
* $_modules = new Modules();
$mods_arr = $_modules->initModules(ROOT.'/app/modules');
*
*/
class Modules
{
public function initModules($modules_base_dir)
{
$modules = array();
//loop over all directories in /app/modules/
foreach (new DirectoryIterator($modules_base_dir) as $dir)
{
if($dir->isDot()) continue;
if($dir->getFilename()=='.svn') continue;
if($dir->isFile()) {
continue;
}
//compile a list of all modules, and load each Module class
$classname = $dir->getFilename();
App::import('modules/'.$classname, $classname);
$module = new $classname();
array_push($modules, $module);
//enumerate all events from BaseModule so we know what we need to handle
$base_events_array = array();
$methods = get_class_methods('BaseModule');
foreach($methods as $method)
{
//strip out any method that starts with "handle_"
if(substr($method, 0, 7)=='handle_')
{
$base_events_array[] = substr($method, 7);
}
}
//IF this module is enabled
if($module->_enabled)
{
//register any EXISTING event handlers for this module
foreach($base_events_array as $event_name)
{
if(method_exists($module, 'handle_'.$event_name))
{
CakeEventManager::instance()->attach(array($module, 'handle_'.$event_name), $event_name);
}
}
//connect up any additional controllers,views, models, bootstraps from this module
App::build(array(
'Config' => array($modules_base_dir.'/'.$classname.'/Config/'),
'Console/Command' => array($modules_base_dir.'/'.$classname.'/Console/Command/'),
'Console/Command/Task' => array($modules_base_dir.'/'.$classname.'/Console/Command/Task/'),
'Lib' => array($modules_base_dir.'/'.$classname.'/Lib/'),
'Lib/Event' => array($modules_base_dir.'/'.$classname.'/Lib/Event/'),
'Controller' => array($modules_base_dir.'/'.$classname.'/Controller/'),
'View' => array($modules_base_dir.'/'.$classname.'/View/'),
'Model' => array($modules_base_dir.'/'.$classname.'/Model/'),
'Vendor' => array($modules_base_dir.'/'.$classname.'/Vendor/')
));
if(file_exists($modules_base_dir.'/'.$classname.'/bootstrap.php'))
{
include_once $modules_base_dir.'/'.$classname.'/bootstrap.php';
}
}
}
//die(var_dump(App::path('Controller')));
return $modules;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
