【问题标题】:How to load a xml file in plugins webroot如何在插件 webroot 中加载 xml 文件
【发布时间】:2015-05-29 12:03:05
【问题描述】:

我正在使用 cakephp-3.0

在插件的一个组件中,我想在插件 webroot 中加载一个 XML 文件。

simplexml_load_file('RangeMessage.xml')

找不到文件。使用

调试路径
debug(realpath(''));

说,实际目录是/path/to/app/webroot。 然而,我的 xml 文件位于 /path/to/app/plugins/myPlugin/webroot 那么如何在不使用绝对路径的情况下找到文件呢?

【问题讨论】:

    标签: cakephp plugins components cakephp-3.0


    【解决方案1】:

    您可以使用Plugin::path() 来检索插件的绝对路径,因此您不必对其进行硬编码。

    Plugin::path('YourPlugin') . 'webroot' . DS . 'RangeMessage.xml'
    

    另请参阅API > Cake\Core\Plugin::path()

    【讨论】:

    • 这很好用。顺便说一句,这是错误还是功能?如果例如javascript 在插件视图中是 importet,它的工作方式与在应用程序中完全相同。
    • @MatthiasMoritz 我真的不明白你指的是什么。指向应用程序 webroot 文件夹的工作目录正是 PHP 的工作方式,它是最初调用的脚本文件 (index.php) 所在的路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2012-12-29
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多