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

Phalcon4 Volt service error - cant set

Hello,

I try to migrate old project to Phalcon4, but i am stuck at volt service. Here is my code in Module.php:


        $di->set('view', function () use ($config) {
            $view = new View();
            $view->setViewsDir($config->modules->{$config->module}->dir.'views/');
            $view->setLayoutsDir('_layouts/');

            $view->registerEngines([
                '.volt' => 'voltService'
            ]);

            $view->registerEngines([
                '.volt' => function (\Phalcon\Mvc\ViewBaseInterface $view, \Phalcon\Di\DiInterface $di) use ($config, $folder) {
                    $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

                    $volt->setOptions([
                        'always' => $config->development,
                        'path' => $config->dirs->cache . 'volt/',
                        'separator' => '_'
                    ]);

                    // Custom filters
                    $compiler = $volt->getCompiler();
                    return $volt;
                }
            ]);

            return $view;
        }, true);

And the error is:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Modules\Admin\Module::Modules\Admin\{closure}(), 1 passed and exactly 2 expected in D:\www\drooble.v3-back\modules\admin\Module.php on line 60

The line 60 is where '.volt' located- so for some reason, second parametter is missing or something. If i remove \Phalcon\Di\DiInterface $difrom the function - volt was started, but give me error about session (it is not accessible).

Some ideas?

edited Oct '19

The issue is function (\Phalcon\Mvc\ViewBaseInterface $view, \Phalcon\Di\DiInterface $di) here- that second parameter. Not the $folder.

Thank u anyway

I think you can remove the 2nd parameter $folder and have a look at https://github.com/phalcon/vokuro/blob/dbe10f654b53457cdcc37a67dea9d7d6f29edcdc/src/Providers/ViewProvider.php#L31

But... u make me think about... i will try something. Thanks.



10.1k
Accepted
answer
edited Oct '19

Something like this should work (didn't try)

$di->set('view', function () use ($config, $di) {
            $view = new View();
            $view->setViewsDir($config->modules->{$config->module}->dir.'views/');
            $view->setLayoutsDir('_layouts/');

            $view->registerEngines([
                '.volt' => function (\Phalcon\Mvc\View $view) use ($config, $di) {
                    $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

                    $volt->setOptions([
                        'always' => $config->development,
                        'path' => $config->dirs->cache . 'volt/',
                        'separator' => '_'
                    ]);

                    // Custom filters
                    $compiler = $volt->getCompiler();
                    return $volt;
                }
            ]);

            return $view;
        }, true);

Yes, i make the same way and its working. Thank u for idea and support.

Now other stuff are broken... for example dispatcher didnt give me -getControllerName sometimes... and... i give up.

Will wait final version :/

Something like this should work (didn't try)

$di->set('view', function () use ($config, $di) {
           $view = new View();
           $view->setViewsDir($config->modules->{$config->module}->dir.'views/');
           $view->setLayoutsDir('_layouts/');

           $view->registerEngines([
               '.volt' => function (\Phalcon\Mvc\View $view) use ($config, $di) {
                   $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

                   $volt->setOptions([
                       'always' => $config->development,
                       'path' => $config->dirs->cache . 'volt/',
                       'separator' => '_'
                   ]);

                   // Custom filters
                   $compiler = $volt->getCompiler();
                   return $volt;
               }
           ]);

           return $view;
       }, true);


10.1k

Have a look a the change log https://docs.phalcon.io/4.0/en/upgrade for all required changes. A lot has changed. Not aware of any dispatcher issues at the moment. Please feel free to open an issue at github if you spotted one.

Ye, mate :) I am on that page from a day... API tab and documentation. But still... i dont know... About dispatcher- i switch to v3 for today couse ot work... and tomorrow will try continue refactor for v4. Will open issue for sure.

Thank again!

Have a look a the change log https://docs.phalcon.io/4.0/en/upgrade for all required changes. A lot has changed. Not aware of any dispatcher issues at the moment. Please feel free to open an issue at github if you spotted one.