Can't access registry in dispatcher

Hi there. I'm trying to port a project from Zend to Phalcon. One thing the original author did is to check the permissions for the requested URL in a custom ACL script, which I'd prefer to not touch (big chaotic magic inside). Now I'm trying to pass some data from the index.php to this ACL script. In index.php I'm doing:

$reg = new \Phalcon\Registry(); $reg->config = $config;

Now inside my Module.php I do have this code:

$dependencyInjector->set('dispatcher', function () { $dispatcher = new Dispatcher();

        $eventsManager = new \Phalcon\Events\Manager();
        $eventsManager->attach("dispatch:beforeDispatch", function($event, $dispatcher) {
            require_once APPLICATION_PATH . "/plugins/acl.php";
        });

(...)

Last but not least inside the acl.php file I do have this code:

$reg = new \Phalcon\Registry(); var_dump($reg->config);

However, the var_dump returns NULL. Looking at the $reg object, it is completly empty. However, when I skip this part and get into the IndexController, the exact same command returns the config object. Isn'tit possible to access the registry in the Dispatcher or am I missing something obvious?

Thanks and regards,

Patrick

edited Mar '16

You create a new Registry instance inside acl.php, so the property you've set in index.php will never show up that way.

// services.php
$dependencyInjector->set('dispatcher', function () use($reg) {
    $dispatcher = new Dispatcher();
    $eventsManager = new \Phalcon\Events\Manager();
    $eventsManager->attach("dispatch:beforeDispatch", function($event, $dispatcher) use($reg) {
        require_once APPLICATION_PATH . "/plugins/acl.php";
    });
});
// acl.php
var_dump($reg->config);

The reference to the original $reg will be carried to the anonymous function with use, so you don't need to instantiate a new one there.

edited Mar '16

But isn't that completly against the purpose of a registry object? I could pass any array through the functions with use, what exactly is the registry object good for?

In Zend I can - no matter where - call this

$registry = Zend_Registry::getInstance ();

and I'll get access to that object. Something useful like that doesn't exist in Phalcon?

That's true, but since you're using an ACL script not part of Phalcon, you'll have to do some hacking...

You could register it with Phalcon DI and use it like this:

// services.php
$di->setShared('registry', function() use($config) {
    $registry = new Registry();
    $registry->config = $config;
    return $registry;
});
// acl.php
$reg = \Phalcon\DI::getDefault()->get('registry');