带有流明的 file_get_contents

Zl3*_*l3n 6 php file-get-contents laravel lumen

我将此代码放入一个函数(php类)中:

$theFile = '/test/test.xml'; // these are in the public folder
dd(file_get_contents($theFile));
Run Code Online (Sandbox Code Playgroud)

如果我去mydomain.local/test/test.xml,我会得到有效的 xml 代码。

但是file_get_contents,我收到此错误:

file_get_contents(/test/test.xml): failed to open stream: No such file or directory
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

car*_*lve 8

Lumen 没有public_path()您在 Laravel 中可能熟悉的方法来轻松获取公共文件的路径。

重新实现它的最简单方法是将一个名为irazasyed/larasupport的包添加到您的项目中,该包添加了各种缺少的帮助程序(包括public_path())以及添加 Lumen 中缺少的供应商发布命令。

或者,如果您不想添加第三方包,只需在您的应用程序目录中创建一个名为helpers.phpcomposer.json文件,然后在您的文件中在“自动加载”部分添加以下内容并运行composer dump-autoload以刷新自动加载器缓存:

"files": [
    "app/helpers.php"
],
Run Code Online (Sandbox Code Playgroud)

然后在里面helpers.php添加以下内容:

<?php
if (!function_exists('public_path')) {
   /**
    * Get the path to the public folder.
    *
    * @param  string $path
    * @return string
    */
    function public_path($path = '')
    {
        return env('PUBLIC_PATH', base_path('public')) . ($path ? '/' . $path : $path);
    }
}
Run Code Online (Sandbox Code Playgroud)