5

I am using a "multi module" MVC structure for my PhalconPHP application.

One issue I am trying to figure out is how I can configure my "Main Layout" view to be above the module view folders.

In other words I want one master "Main Layout" (as described here) and I want all my modules to output their views at "Controller View" level within that main layout view.

At default it appears the Main Layout view is being taken from

[app]
 [module1]
    [controllers]
    [models]
    [views]
        (main layout is coming from here)
 [module2]
    [controllers]
    [models]
    [views]
        (main layout is coming from here)
 [views]
    (master main layout should come from here?)

I hope this makes sense!

Tim
  • 3,091
  • 9
  • 48
  • 64

3 Answers3

2

What you are looking for cannot be done at this version (0.5.0 stable) or the next one 0.6.0 (since it is frozen, pending release).

In your module you register your views

// /module1/Module.php

// Registering the view component
$di->set(
    'view', 
    function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../apps/module1/views/');
        return $view;
    }
);

// /module2/Module.php

// Registering the view component
$di->set(
    'view', 
    function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../apps/module2/views/');
        return $view;
    }
);

and so on.

You can also have a master view that will be common for all modules, but not a combination of the two.

//Registering a shared view component
$di->set(
    'view', 
    function() {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../apps/views/');
    return $view;
    }
);

See this example on Github.

This could very well be a NFR for the 0.7 version.

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
  • Hi Nikolaos, This is actually how I have it set up currently after using https://github.com/phalcon/mvc/tree/master/multiple as a guide. In its current form it is not looking at the apps/views/ folder. Do I need to add something to the bootstap file? – Tim Oct 19 '12 at 11:22
  • I have tried adding the following to my bootstrap file $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; }); $this->setDI($di); But unfortunately it doesn't work. – Tim Oct 19 '12 at 23:19
  • Tim, I have changed the response. Please have a look. – Nikolaos Dimopoulos Oct 21 '12 at 20:49
  • Nikolaos, thank-you so much for continuing to bear with me! I checked out the github example and noticed those modules didn't have the di view set code in the module.php file. I also noticed that all the view files (including for the modules) were in the "common" directory. I thought the point of using modules was to be able to build self contained 'modules' that function independently of each other. Having all the view files in a common directory seems to miss the point. Is there no other way? Where can I request this for 1.7. I really do appreciate your help! – Tim Oct 22 '12 at 07:07
  • Tim, sorry for the late reply. Unfortunately what you are asking cannot be achieved for the moment - I have confirmed this with code and by talking to the developers. This could very well be a useful feature so I will open a NFR for 0.7.0 onward. 0.7.0 is pretty much filled but we will see what we can do :) – Nikolaos Dimopoulos Oct 24 '12 at 13:54
1

At Phalcon ver 1.2.4(maybe in earlier versions too) one master "Main Layout" is possible. Phalcon builds lauout's path relatively a ViewsDir, that sets like

$view->setViewsDir('../apps/views/');

So, if set lauout's path relatively that, it's will work

$view->setLayoutsDir('./../../views/');

Maybe the best way to organise that structure to declare view object at application initialization and when in Module.php set ViewsDir:

// Application.php
$di->set('view', function() use ($config) {
    $view = new View();
    $view->setLayoutsDir('./../../views/');
    $view->setLayout('index');
}, true);

and

// /module1/Module.php
$di->get('view')->setViewsDir('../apps/module1/views/');
0

You can use multiple-shared-layouts Demo or download to see how it works https://github.com/phalcon/mvc/tree/master/multiple-shared-layouts/apps

Or Just add tfor each Module.php

 $di['view'] = function () {
            $view = new View();
            $view->setViewsDir(__DIR__ . '/views/');
            $view->setLayoutsDir('../../common/layouts/');
            $view->setTemplateAfter('main');
            return $view;
        };
rzx10r
  • 134
  • 1
  • 1
  • 9