We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Reuse and update registered services in other modules

Hello Partypeople,

I have a Multiple-Shared-Views MVC structured Phalcon Project. Everything is in a nice module and have a order.

But! My view filters and functions for the whole project are registered in the common module (my base module), lets call it viewService. I would like to register just the important things of viewService in my common module, like a filter for dates (used in almost every module). After that I want to register the module specific filter & functions (like currency translation) within my already registered viewService in the affected module.

So I imagend somthing like:

CommonModule:

$di->setShared('view', function () use ($di) {
            $config = $di->get('config');
            $view = new View();

            $view->registerEngines([
                '.volt'  => function ($view, $di) use ($config) {
                    .
                    .
                    $compiler->addFilter('date', 'Format::date');
                    .
                    .
            }
}

AffectedModule:

$di->getShared('view', function () use ($di) {
            $registeredView = getRegisteredView();

            $registeredView->addEngines([
                '.volt'  => function ($view, $di) use ($config) {
                    $compiler->addFilter('price', function ($price) { return "Format::price($price, ',', '.')"; });
                    .
                    .
                    .
            }
}

Unfortunately I didn't find any solution for my problem. If I try to get the .volt array from $di->getShared("view")->getRegisteredEngines() , I get only an empty array.

I hope someone can help me out.

Modules are not sharing services registered in them. You just need to register service outside of module.

Thank you Wojciech for your reply. You are right, I can't share service from a module with an other module. So I think, that I can't implement an solution, that brings a benefit to my project, because of this "problem". I think its ok, if I leave it so.

FYI: My affected modules are extending my common module. So my affected modules have access to my registered services in my common module. But how I told: "If I try to get the .volt array from $di->getShared("view")->getRegisteredEngines() , I get only an empty array."

edited Jun '17

What you mean you have empty array? Check what $di->getShared("view")->getRegisteredEngines() returns. Also getShared doesn't accept second argument as function, also there is no addEngines method in view.