I'm using the symfony/event-dispatcher component and am trying to get a 'hello world' thing to work, I think.
My application (not symfony2) runs bootstrap.php
then MVC's its way to some file I named composer.php
.
bootstrap.php (the necessary parts)
<?php
require __DIR__ . '/vendor/autoload.php';
use Events\MsgEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
$dispatcher = new EventDispatcher();
if ($user_id) {
$dispatcher->dispatch('msg_event', new MsgEvent("I'm logged in as: " . $user['username']));
}
composer.php
<?php
use Events\MsgEvent;
$dispatcher->addListener('msg_event', function (MsgEvent $event) {
echo $event->getMsg();
});
$dispatcher->dispatch('msg_event', new MsgEvent("I got called from composer.php"));
On the route for my composer.php page I get to see 'called from composer', but my 'logged in as' message doesn't appear. When I output it's return, I can see it has succesfully created the event.
I have a feeling the problem is that the listener is defined after the first dispatch and somehow this does not get stored in the dispatcher. But I'm not sure.
What am I not seeing here?
index.php
includesbootstrap.php
which gives a$controller
, thenlayout.php
gets included and in there there's a call to$controller->page
which will include the final view/composer.php