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

Partial is not working after upgrading to Phalcon 3

Hi upgraded to Phalcon 3 and I got error in my existing project.

I have registered the partial directory in index.php like this

$di->set('partials', function() {
                $partials = new View();
                $partials->setPartialsDir('../apps/common/views/');
                return $partials;
            });

And header.phtml is in same directory as defined above.

In my index.phtml I have following code

$this->partials->partial("header");

but however above code is giving me following error and have no Idea how to solve it.

Fatal error: Uncaught Exception: The argument is not initialized or iterable() in phalcon/mvc/view.zep:694 Stack trace: #0 [internal function]: Phalcon\Mvc\View->_engineRender(Array, '../apps/common/...', false, false) #1 D:\server\www\example.com\frontend\apps\modules\property\views\index.phtml(2): Phalcon\Mvc\View->partial('header') #2 [internal function]: unknown() #3 [internal function]: Phalcon\Mvc\View\Engine\Php->render('../apps/modules...', Array, true) #4 [internal function]: Phalcon\Mvc\View->_engineRender(Array, 'index', true, true, NULL) #5 [internal function]: Phalcon\Mvc\View->render('index', 'index', Array) #6 D:\server\www\example.com\frontend\public\index.php(485): Phalcon\Mvc\Application->handle() #7 D:\server\www\example.com\frontend\public\index.php(497): RealEstate\MyApplication->main() #8 {main} thrown in phalcon/mvc/view.zep on line 694

What is this error? how it occurred?



85.5k

i do it like this:

$di->set('view', function() {
            $view = new View();
            $view->setViewsDir(MODULES_DIR . 'home/views/')
                ->setLayoutsDir(LAYOUT_DIR)
                ->setPartialsDir(PARTICIALS_DIR) // <<----
                ->setTemplateAfter('main')
                ->registerEngines(['.phtml' => 'Phalcon\Mvc\View\Engine\Php']);
            return $view;
        });

and then in my views/layout i am able to do

$this->partial('menu');

in your case i see no reason why registering another service for no real reason ?

I have multiple modules, so I have registered view in each module file Module.php instead of in index.php. Where as all modules share same partial so I have registered it in index.php

i do it like this:

$di->set('view', function() {
          $view = new View();
          $view->setViewsDir(MODULES_DIR . 'home/views/')
              ->setLayoutsDir(LAYOUT_DIR)
              ->setPartialsDir(PARTICIALS_DIR) // <<----
              ->setTemplateAfter('main')
              ->registerEngines(['.phtml' => 'Phalcon\Mvc\View\Engine\Php']);
          return $view;
      });

and then in my views/layout i am able to do

$this->partial('menu');

in your case i see no reason why registering another service for no real reason ?



85.5k

what i posted was from my module.php, here is the full file

Module.php

<?php

namespace Shop\Home;

use \Phalcon\Loader,
    \Phalcon\DI,
    \Phalcon\Mvc\View,
    \Phalcon\DiInterface,
    \Shop\Application\ApplicationModule;

class Module extends ApplicationModule {

    public static function initRoutes(\Phalcon\DiInterface $di) {

        $loader = new Loader();

        $loader->registerNamespaces([
            'Shop\Home' => __DIR__,
            ], true)
            ->register();

        $router = $di->getRouter();

        $router->mount(new ModuleRoutes());
    }

    public function registerAutoloaders(\Phalcon\DiInterface $di = null){

        $loader = new Loader();
        $loader->registerNamespaces([
                'Shop\Home' => __DIR__,
                'Shop\Home\Controllers' => __DIR__ . '/controllers/',
                'Shop\Home\Models' => __DIR__ . '/models/',
                'Shop\Home\Library' => __DIR__ . '/lib/',
                'Shop\Home\Forms' => __DIR__ . '/forms/',
            ], true)
            ->register();
    }

    public function registerServices(\Phalcon\DiInterface $di = null){

        $dispatcher = $di->get('dispatcher');
        $dispatcher->setDefaultNamespace('Shop\Home\Controllers\\');

        $di->set('view', function() {
            $view = new View();
            $view->setViewsDir(MODULES_DIR . 'home/views/')
                ->setLayoutsDir(LAYOUT_DIR)
                ->setPartialsDir(PARTICIALS_DIR)
                ->setTemplateAfter('main')
                ->registerEngines(['.phtml' => 'Phalcon\Mvc\View\Engine\Php']);
            return $view;
        });
    }
}