code igniter 2.0.3 loader add_package_path 视图级联

发布于 2024-12-10 08:50:13 字数 2473 浏览 0 评论 0原文

我不确定我是否理解错误,但如果我理解错误,请纠正我。我正在使用 Code Igniter 的 Loader 类来加载应用程序“包”。它在大多数情况下工作正常。然而,让我困惑的一件事是视图路径如何工作(或不工作)。

在文档中(http://codeigniter.com/user_guide/libraries/loader.html ),它说:

“默认情况下,调用 add_package_path() 时会设置包视图文件路径。视图路径会循环遍历,一旦遇到匹配,就会加载视图。

在这种情况下,可能会出现视图命名冲突之内包发生,并且可能加载不正确的包,为了防止这种情况,请在调用 add_package_path() 时设置可选的第二个参数 FALSE。”

这是他们给出的示例代码:

$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is TRUE

// Reset things
$this->load->remove_package_path(APPPATH.'my_app');

// Again without the second parameter:
$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Loads

我认为示例代码中存在拼写错误,他们的意思是将 FALSE 传递给对 add_package_path() 的第二次调用。我对此的解释是,如果传入 FALSE,则应跳过包的视图路径并应使用原始视图路径。

但是,当我实际尝试传入 FALSE 时,我仍然从外部包获取视图(视图名称同时存在于外部包和当前应用程序中)。查看/system/core/Loader.php中的Loader类,这是add_package_path()的定义:

public function add_package_path($path, $view_cascade=TRUE)
{
    $path = rtrim($path, '/').'/';

    array_unshift($this->_ci_library_paths, $path);
    array_unshift($this->_ci_model_paths, $path);
    array_unshift($this->_ci_helper_paths, $path);

    $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;

    // Add config file path
    $config =& $this->_ci_get_component('config');
    array_unshift($config->_config_paths, $path);
}

它的作用是将包视图路径添加到视图路径数组的前面,值为TRUE或FALSE。在实际加载视图的函数中,也在 Loader 类中并调用 _ci_load($_ci_data) ,这是它选择要查看的视图路径的段:

foreach ($this->_ci_view_paths as $view_file => $cascade)
{
    if (file_exists($view_file.$_ci_file))
    {
        $_ci_path = $view_file.$_ci_file;
        $file_exists = TRUE;
        break;
    }

    if ( ! $cascade)
    {
        break;
    }
}

在我看来,如果视图存在于外部包中(因为它将位于调用 add_package_path 的数组的起始位置),无论 add_package_path 中的第二个参数是 TRUE 还是 FALSE,都会加载它。没有太多关于此的文档,而且我没有看到为此提交的任何错误报告。同样,我的解释是,如果传递给 add_package_path() 的第二个参数为 FALSE,则应跳过包的查看路径。我认为在 _ci_load() 中,级联检查内部应该是继续而不是中断,并且它应该出现在文件检查之前。

这是我应该报告的错误还是我在这里错误地解释了某些内容并且该功能正在正常工作?

I'm not sure if I'm interpreting this wrong but please correct me if I am. I'm using Code Igniter's Loader class to load an application "package". It is working fine for the most part. However, one thing that's tripping me up is how the view paths are working (or not working).

In the documentation (http://codeigniter.com/user_guide/libraries/loader.html), it says:

"By Default, package view files paths are set when add_package_path() is called. View paths are looped through, and once a match is encountered that view is loaded.

In this instance, it is possible for view naming collisions within packages to occur, and possibly the incorrect package being loaded. To ensure against this, set an optional second parameter of FALSE when calling add_package_path()."

This is the example code they give:

$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Will not load the default welcome_message b/c the second param to add_package_path is TRUE

// Reset things
$this->load->remove_package_path(APPPATH.'my_app');

// Again without the second parameter:
$this->load->add_package_path(APPPATH.'my_app', TRUE);
$this->load->view('my_app_index'); // Loads
$this->load->view('welcome_message'); // Loads

I think there is a typo in the example code and they mean to pass in FALSE to the second call to add_package_path(). My interpretation of this is that, if you pass in FALSE, the view path of the package should be skipped and the original view path should be used.

However, when I actually try to pass in FALSE, I am still getting the view from the external package (the view name exists in both the external package and the current application). Looking at the Loader class in /system/core/Loader.php, this is the definition of add_package_path():

public function add_package_path($path, $view_cascade=TRUE)
{
    $path = rtrim($path, '/').'/';

    array_unshift($this->_ci_library_paths, $path);
    array_unshift($this->_ci_model_paths, $path);
    array_unshift($this->_ci_helper_paths, $path);

    $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;

    // Add config file path
    $config =& $this->_ci_get_component('config');
    array_unshift($config->_config_paths, $path);
}

What it does is add the package view path to the front of the view paths array, with a value of TRUE or FALSE. In the function that actually loads the views, also in the Loader class and called _ci_load($_ci_data), this is the segment where it chooses the path of views to look in:

foreach ($this->_ci_view_paths as $view_file => $cascade)
{
    if (file_exists($view_file.$_ci_file))
    {
        $_ci_path = $view_file.$_ci_file;
        $file_exists = TRUE;
        break;
    }

    if ( ! $cascade)
    {
        break;
    }
}

It seems to me that if the view exists in the external package (since it will be at the from of the array from the call to add_package_path), it will be loaded regardless of whether the 2nd parameter in add_package_path is TRUE or FALSE. There's not too much documentation on this and I didn't see any bug reports filed for this. Again, my interpretation is that if the 2nd parameter passed in to add_package_path() is FALSE, the view path of the package should be skipped. I'm thinking that in _ci_load(), inside the check for cascade should be a continue instead of a break, and it should come before the check for the file.

Is this a bug that I should report or am I interpreting something incorrectly here and the function is working as it should be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-12-17 08:50:13

迄今为止,该拼写错误仍然存​​在!第二个代码示例中的第二个参数应该是 FALSE。

我对用法的解释是:

如果第二个参数为 TRUE,则视图将仅从外部包加载。

如果第二个参数为 FALSE,则视图将从外部包或本地存储库加载,具体取决于找到它的位置(首先检查外部包)。这个想法是,如果它不在外部包中,则应在本地存储库中继续搜索。

The typo exists to this date as well! The second argument in the second code example should have been FALSE.

My interpretation of the usage is:

If the second argument is TRUE, then the view will ONLY be loaded from the external package.

If the second argument is FALSE, then the view will be loaded from the external package or the local repo depending on where it is found (checking the external package first). The idea being that if it's not in the external package, the search should continue in the local repo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文