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

Render layout twice when use multiple view directories

I just upgrade to Phalcon3.0.1

My project structure like this:

project
|-- mod1
|   |-- controllers
|   |   |-- FirstController.php
|   |-- views
|   |   |-- first
|   |   |   |-- index.phtml
|   |-- Bootstrap.php
|-- mod2
|   |-- controllers
|   |   |-- SecondController.php
|   |-- views
|   |   |-- second
|   |   |   |-- index.phtml
|-- views
|   |-- layouts
|   |   |-- layout.phtml
|   |-- main.phtml
|-- config
|   |-- serivces.php

I set up $di['view'] in services.php and mod1/Bootstrap.php both

In services.php

$di->setShared('view', function () {
    $config = $this->getService('config')->resolve();
    $view = new View();
    $view->setViewsDir( [$config->application->viewsDir] )
         ->setLayoutsDir( $config->application->layoutsDir )
         ->setLayout( $config->application->layoutName );
    return $view;
});

In mod1/Bootstrap.php

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

        /**
         * Update the view component
         */
        $v = $di->get('view');
        $path = $v->getViewsDir();
        $path[] = __DIR__ . '/views/';
        $v->setViewsDir( $path );
        $di->setShared('view' , $v);
}

But it will render layouts/layout.phtml twice and mod1/views/first/index.phtml one time

Why??



85.5k

bacause of this commit here, it fucks it up https://github.com/phalcon/cphalcon/commit/e9968fe0460036d74d67d4bea35a76479b1d5777

i have a fork with this merge removed cuz of this



4.0k

Thank for your reply

But I have tried to change phalcon/mav/view.zep at line 694

if notExists === true {
            /**
             * Notify about not found views
             */
            if typeof eventsManager == "object" {
                let this->_activeRenderPaths = viewEnginePaths;
                eventsManager->fire("view:notFoundView", this, viewEnginePath);
            }

            if !silence {
                throw new Exception("View '" . viewPath . "' was not found in any of the views directory");
            }
        }

to

if notExists === false { break; }
            /**
             * Notify about not found views
             */
            if typeof eventsManager == "object" {
                let this->_activeRenderPaths = viewEnginePaths;
                eventsManager->fire("view:notFoundView", this, viewEnginePath);
            }

            if !silence {
                throw new Exception("View '" . viewPath . "' was not found in any of the views directory");
            }

and rebuild

After that it's still not work

Could you explain how you to do ?



85.5k
edited Sep '16

you should add


  if notExists === false {
      break;
   }

https://github.com/phalcon/cphalcon/blob/e9968fe0460036d74d67d4bea35a76479b1d5777/phalcon/mvc/view.zep#L716

here or 1 line above i am not 100% sure, turns out i resetnly have screwed my fix in my fork ...