流明的file_get_contents [英] file_get_contents with Lumen
问题描述
我将此代码放入一个函数中(php类):
I have this code into a function (php class) :
$theFile = '/test/test.xml'; // these are in the public folder
dd(file_get_contents($theFile));
如果我去mydomain.local/test/test.xml
,我会得到有效的xml代码.
If I go to mydomain.local/test/test.xml
, I get the working xml code.
但是使用file_get_contents
时,出现此错误:
But with file_get_contents
, I get this error :
file_get_contents(/test/test.xml): failed to open stream: No such file or directory
如何解决这个问题?
推荐答案
在Laravel中,Lumen没有您可能不熟悉的public_path()
来轻松获取公共文件的路径.
Lumen doesn't have the public_path()
that you might be familiar with in Laravel to easily grab the path to a public file.
重新实现它的最简单方法是将一个名为 irazasyed/larasupport 的软件包添加到您的该项目将添加各种缺少的帮助程序(包括public_path()
),并添加流明缺少的供应商发布命令.
The simplest method for re-implementing it would be to add a package called irazasyed/larasupport to your project which adds various missing helpers (including public_path()
) as well as adding the vendor publish command that is missing from Lumen.
或者,如果您不想添加第三方程序包,只需在应用程序目录中创建一个名为helpers.php
的文件,然后在您的composer.json
文件中,在自动加载"部分中添加以下内容并运行composer dump-autoload
即可刷新自动加载器缓存:
Alternatively if you do not wish to add a third party package simply create a file in your app directory called helpers.php
and then within your composer.json
file add the following within the "autoload" part and run composer dump-autoload
to refresh the autoloader cache:
"files": [
"app/helpers.php"
],
然后在helpers.php
中添加以下内容:
Then within helpers.php
add the following content:
<?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);
}
}
这篇关于流明的file_get_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!