本文是小编为大家收集整理的关于如何在一个多模块的Phalcon应用程序中使用 "主布局 "视图?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我正在为我的phalconphp应用程序使用"多模块" MVC结构.
我试图弄清楚的一个问题是如何将"主布局"视图配置为 模块视图文件夹.
换句话说,我想要一个主"主布局"(>如下所述),我希望我所有的模块能在该主要布局视图中输出其视图.
默认情况下,似乎是从
中获取的主要布局视图[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?)
我希望这是有道理的!
推荐答案
您要寻找的是在此版本(0.5.0稳定)或下一个0.6.0(因为它被冻结,待定释放).
在您的模块中您注册您的视图
// /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; } );
等等.
您还可以具有所有模块中常见的主视图,而不是两个模块的组合.
//Registering a shared view component $di->set( 'view', function() { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/views/'); return $view; } );
参见 github.
这很可能是0.7版本的NFR. 在Phalcon Ver 1.2.4(也许也在早期版本中)一个主"主布局"是可能的.
phalcon构建了劳奥特的路径相对的视图dir,它设置为 因此,如果设置Lauout的途径相对途径,它将起作用 也许是整理该结构以在应用程序初始化时声明对象的最佳方法
和 您可以使用多个共享的layout演示或下载以查看其工作原理
主/多共享的layouts/apps 或仅添加每个模块的t t for t for t for t for t t.其他推荐答案
$view->setViewsDir('../apps/views/');
$view->setLayoutsDir('./../../views/');
// Application.php
$di->set('view', function() use ($config) {
$view = new View();
$view->setLayoutsDir('./../../views/');
$view->setLayout('index');
}, true);
// /module1/Module.php
$di->get('view')->setViewsDir('../apps/module1/views/');
其他推荐答案
$di['view'] = function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('../../common/layouts/');
$view->setTemplateAfter('main');
return $view;
};
问题描述
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!
推荐答案
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.
其他推荐答案
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/');
其他推荐答案
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; };