3

I'm modelling a login page using different schemas (basic username & password combination and another schema using a Yubikey, for now).

My controller looks like this:

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

The authentication schema looks like this:

/**
 * 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 */
                ]
            ]
        ]
    ]
]);

The problem I have is include_once() is not working for me, as the $data variable isn't really seen neither by the authentication schema nor the opposite (the document namespace seeing any content coming from the authentication schema on inclusion).

However, if I use include() it works. Maybe the problem lies within the use of a namespace and including external content. I never use the include() function as I always like to check if the script is already included, even if it has bit of a performance penalty for the extra check.

Maybe I'm not fully understanding how namespaces work in PHP or I did something weird with the array_merge_recursive() function, but the more I look at the code, the less I find about something potentially wrong and I feel a bit lost.

Can anyone help me figuring this?

4

1 回答 1

1

显示了一个简单的脚本设置,命名空间内的 include_once 实际上与 include 相同。看来,您之前在其他地方包含了 schema.php。

<?php

namespace Document {
    include_once "data.php";

    echo "Hello " . $data;
}

数据.php

<?php

$data = "World";

在您的情况下,您可以添加一些调试输出,以获取第一次包含您的方案的文件/行。IE

var_dump(array_map(function($x) {
    return [$x['file'], $x['line']];
}, debug_backtrace()));

但我建议只使用include而不是include_once返回数据,而不是创建新的变量。

即scheme.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");

并在需要的地方进行合并(和其他事情)。

于 2016-03-21T12:45:53.463 回答