我们正在使用Zend框架。我们正在从管理面板或在运行时创建模块,但其布局不起作用,因为我们需要在应用程序中定义以下代码.ini
pizzahut.resources.layout.layout = "layout"
pizzahut.resources.layout.layoutPath = APPLICATION_PATH "/modules/pizzahut/layouts/scripts/"
我们是否可以在运行时使用 PHP 编辑我们的应用程序.ini,或者我们可以使用 PHP 在应用程序中添加新的布局配置.ini?
在谷歌搜索之后,我发现我们可以用PHP编辑我们的application.ini
。
这是代码:
$config = parse_ini_file(
APPLICATION_PATH . "/configs/application.ini",
TRUE,
INI_SCANNER_RAW
);
$config["production"]["$store.resources.layout.layout"] = "layout";
$layoutPath = 'APPLICATION_PATH "/modules/' . $store . '/layouts/scripts/"';
$config["production"]["$store.resources.layout.layoutPath"] = $layoutPath;
$result = Helper_common::write_ini_file(
$config,
APPLICATION_PATH . "/configs/application.ini",
TRUE
);
此处parse_ini_file
用于从带有常量的application.ini
文件中检索内容。
write_ini_file
是我调用的用于重写application.ini
文件的函数。
以下是write_ini_file
函数:
public static function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]'n";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = ".$elem2[$i]."'n";
}
}
else if($elem2=="") $content .= $key2." = 'n";
else $content .= $key2." = ".$elem2."'n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key2."[] = ".$elem[$i]."'n";
}
}
else if($elem=="") $content .= $key2." = 'n";
else $content .= $key2." = ".$elem."'n";
}
}
if (!$handle = fopen($path, 'w')) {
return false;
}
if (!fwrite($handle, $content)) {
return false;
}
fclose($handle);
return true;
}
ini 文件在应用程序开始运行时加载,在引导之前,所以不,你不能。
查看这些内容以了解如何在运行时动态更改布局:
http://vandenbos.org/zend-framework-module-specific-layout/
在引导程序中更改布局
http://forums.zend.com/viewtopic.php?f=69&t=2466&start=0
从引导程序设置 Zend 布局
在 ZEND 中设置引导程序内部模块的布局文件