I want to use Symfony EventDispatcher in Zend Framework 1.11

424 Views Asked by At

I want(need) to use Symfony EventDispatcher inside Zend Framework 1.11 I have the EventDispatcher being loaded in the Bootstrap of zf...

public function run()
    {
        require APPLICATION_PATH . 
                '/../library/Symfony/Component/EventDispatcher/EventDispatcher.php';
        $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();

        $dispatcher->dispatch("bootstrap.prerun", null);

        parent::run();

        $dispatcher->dispatch("bootstrap.postrun", null);

    }

My question is, how can I use it inside the ZF controllers ?

Thank you for any help. Best regards,

1

There are 1 best solutions below

0
On

I'm replying to this because my problem was understanding how I could integrate both frameworks so I could have the EventDispatcher of Symfony working on all my ZF application including modules.

This was my solution: On the bootstrap..

public function run()
{
    $dispatcher = $this->getResource("Far_Resource_EventDispatcher");

    $dispatcher->dispatch("bootstrap.prerun", null);

    $listener = new DM_Service_StockListener();

    $dispatcher->addListener(
            "equipment.new", 
            array($listener, 'onEquipment') 
            );

    parent::run();

    $dispatcher->dispatch("bootstrap.postrun", null);

}

I created a resource for EventDispatcher to be instantiated this way it will be globally accessible by the bootstrap and can be started has a resource. The resource code is:

class Far_Resource_EventDispatcher extends Zend_Application_Resource_ResourceAbstract
{
    private $_dispatcher = null;

    public function init()
    {
        if ($this->_dispatcher === null ) {
            require APPLICATION_PATH . 
                '/../library/Symfony/Component/EventDispatcher/EventDispatcher.php';

            $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
        } else {
            $dispatcher = $this->_dispatcher;
        }
        $this->getBootstrap()->eventDispatcher = $dispatcher;

        return $dispatcher;

    }
}

And in the application.ini I setup

pluginPaths.Far_Resource_EventDispatcher = APPLICATION_PATH "/../library/Far/Resource/EventDispatcher.php"
resources.Far_Resource_EventDispatcher = true;

I used this example on how to create a resource: http://blog.madarco.net/327/how-to-make-your-own-zend-framework-resource-plugin/

I'm still working in this but looks like it's working well on the first test...