How do I use “Main Layout” views in a multi module Phalcon application?

不想你离开。 提交于 2019-12-19 08:07:14

问题


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!


回答1:


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.




回答2:


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/');



回答3:


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;
        };


来源:https://stackoverflow.com/questions/12951048/how-do-i-use-main-layout-views-in-a-multi-module-phalcon-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!