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

Multi-module Single Layout

Hello,

I'm trying to setup a single global layout for a multi-module setup (based on MVC Multi-shared Layouts).

I've defined a services.php bootloader where I configure my view service:

$di['view'] = function() {
  $view = new View();

  $view->setLayoutsDir(__DIR__ . '/../resources/layouts/');  
  $view->setTemplateAfter('main');

  $view->registerEngines([
    ".volt" => function ($view, $di) {
      $volt = new Volt($view, $di);

      $volt->setOptions(array(
        'compiledPath' => __DIR__ . "/../storage/cache/volt/",
        'compiledSeparator' => '_',
        'compileAlways' => true
      ));

      return $volt;
    }
  ]);

  return $view;
};

On my Module.php, I've defined my module views directory:

public function registerServices (DiInterface $di) {
  $di->get('view')->setViewsDir(__DIR__ . '/views/');
}

My main problem is that the main.volt layout is not loading prior to my module view. I don't get any error, but I can't get my view wrapper inside my main layout. Only the module view is displayed.

Anyone knows a workaround or a better way to achieve this?

Thank you.

instead of

$view->setLayoutsDir(__DIR__ . '/../resources/layouts/');  
$view->setTemplateAfter('main');

define the views path and set the default layout

$view->setViewsDir(__DIR__ . '/../resources/');
$view->setLayout('main');

The default folder for layout is layouts relative from views path.

That would only apply if I have my layout on the same place I have my views files, right?

But, what if I don't have my layouts files/folder on the same place I have my module views? Example:

. app
    . modules
        . moduleA
            . views
                . index.volt
            . Module.php
    . config
        . services.php
. resources
    . layouts
        . main.volt

In this case you can use setLayoutsDir to define your layouts folder then use setLayout to define the default, that should work.

It seems not to be working. I've defined the LayoutsDir and setLayout on both services.php bootloader and on the module Modules.php.. It stills only showing the module view and not the layout with the model view content printed..Am I missing something else?

This could a configuration problem somewhere. Can you try my lib ? You can set up an app with default layout.
https://github.com/corentin-begne/phalconTool https://github.com/corentin-begne/phalconTool/blob/master/templates/project/app/config/services.php Then when the app is created you can try to change the layouts dir to make it in a differente folder of the views, never tested but should work.

The problem is that it seems my app is just ignoring the layout file, instead of using it and render the module view content inside of it. When I delete or change the layout file dir or name, I get an error saying it cannot find the layout.

And my layout file is as simple as:

<!DOCTYPE html>
<html>
    <head>
        <title>APP</title>
    </head>
    <body>
           <?php echo $this->getContent(); ?>
    </body>
</html>