Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Multi-module - Using hierarchical module layout loading.

Hi, what im trying to acomplish is something like this:

i have the directory structure App root, has two childs folders, common and modules.

Module folder and two modules, Module A, Module B, each module has his respective views, layouts folders

Common folder has layouts, and views folders.

e.g: if phalcon looks for /app/module/moduleA/views/foo.volt and is not found, looks for /app/common/views/foo.volt

I do that overriding the render method from Phalcon\Mvc\View\Engine\Volt

It works so good, except for layouts, if i put a name that do not exist in moduleA/layout do not pass render method, and cant look for the common one because of that.

I dont know how to handle this problem, any clues will be appreciated.

If you have some disgusting time reading this text because of grammar, sorry im not english native.



60.8k


381
Accepted
answer
edited Dec '16

hey merry x-mas @izo, yes i see that multiple-shared-layouts, works well, but this approach is different, the common layout is a fallback when you dont have a module/layout file.

i think is not the most elegant solution but i solve it overriding setLayout() in View class:

we assume that always layout directory is pointing to modules/modulename/layout, so :

class MyView extends View {

    public function setLayout($name){

        if(!file_exists($this->getLayoutsDir().$name.".volt") ){
            $this->setLayoutsDir( /* common dir value here */ );
        }
        parent::setLayout($name);
    }
}

Then i replace View to MyView in service view, and works, if anyone has a better approach, im listening



60.8k

i think you can set multiple layoutrs dir

whereever you define your services

$view->setLayoutDir([ 
    "/var/www/a/layouts/",
    "/var/www/a/whatever"
]);

but i am not 100% sure

TY @Izo, im gonna try that

i think you can set multiple layoutrs dir

whereever you define your services

```

$view->setLayoutDir([ "/var/www/a/layouts/", "/var/www/a/whatever" ]);

```

but i am not 100% sure

I try that, but it throws an error "array to string conversion", i stick to my solution, thanks anyway