我正在使用不同的模式(基本的用户名和密码组合以及现在使用Yubikey的另一个模式)对登录页面进行建模。
我的控制器如下所示:
namespace Document {
/**
* get the current authentication schema
*/
$schema = 'Modules'Backend'Authentication::getSchema();
/**
* initialize the template data
*/
if (empty($data)) {
$data = [];
}
/**
* include the document content block
*/
$data = array_merge_recursive($data, [
"document" => [
"sections" => [
/* further content goes here */
]
]
]);
/**
* include the authentication schema content block
*/
if (file_exists($schema = "{$_SERVER["DOCUMENT_ROOT"]}/pages/controllers/backend/login/{$schema}.php")) {
include_once($schema);
}
/**
* output the document content
*/
echo 'Helpers'Templates::getTemplate("backend/pages/login", $data);
/**
* free all used resources
*/
unset($data, $schema);
}
身份验证架构如下所示:
/**
* include the document content block
*/
$data = array_merge_recursive(!empty($data) ? $data : [], [
"document" => [
"sections" => [
"schema" => [
"content" => [
"token" => 'Helpers'Strings::getToken(),
/* rest of content block goes here */
],
"strings" => [
"title" => _("This is a sample string"),
/* rest of translation strings block goes here */
]
]
]
]
]);
我遇到的问题是include_once()
对我不起作用,因为身份验证架构或相反的 (文档命名空间在包含时看到来自身份验证架构的任何内容)都没有真正看到 $data
变量)。
但是,如果我使用include()
它可以工作。也许问题在于命名空间的使用并包含外部内容。我从不使用 include()
函数,因为我总是喜欢检查脚本是否已经包含在内,即使它对额外检查有一点性能损失。
也许我没有完全理解命名空间在 PHP 中的工作原理,或者我对 array_merge_recursive()
函数做了一些奇怪的事情,但我越看代码,我就越少发现潜在的错误,我感到有点迷茫。
谁能帮我弄清楚这一点?
显示的脚本的简单设置include_once命名空间内的工作方式与包含相同。所以看起来,你之前在其他地方包含了模式.php。
<?php
namespace Document {
include_once "data.php";
echo "Hello " . $data;
}
数据.php
<?php
$data = "World";
在您的情况下,您可以添加一些调试输出,以获取文件/行,其中第一次包含您的方案。 即
var_dump(array_map(function($x) {
return [$x['file'], $x['line']];
}, debug_backtrace()));
但是我建议只使用include
而不是include_once
并返回数据,而不是创建新的变量。
即方案.php
return [
"document" => [
"sections" => [
"schema" => [
"content" => [
"token" => 'Helpers'Strings::getToken(),
/* rest of content block goes here */
],
"strings" => [
"title" => _("This is a sample string"),
/* rest of translation strings block goes here */
]
]
]
]
];
后来将其包含在
$data = include($schema . '.php");
并在您需要的地方进行合并(和其他内容)。