我有一个本地文件,需要包含在一个开始的Silex应用程序中。我刚开始,我有所有的锅炉板代码设置,尤其是在这里。
// I need to return test.php for this route
$app = new Silex'Application();
$app->get('/', function() use($app) {
return $string; // put the file in a string
});
$app->run();
经过一些谷歌搜索,我发现了这些文章:SO,PHP.net
所以看起来我可以使用file_get_contents()
,如果我需要评估代码,我可以将其封装在eval()
中。或者我可以使用链接中显示的其他方法。
我希望只使用类似于require()
的东西,但Silex需要返回一个字符串。我想我可以只写我自己的助手函数parseFile()
,但这必须在其他地方完成吗?
更新
// this does not work, I'v verified the path is correct.
return file_get_contents($path, TRUE);
文档有点难以导航,但这里有适当的锚-
Sensio Labs文档
正如你所建议的,还有一种更直接的方法。
您基本上需要使用名称恰当的sendFile()
方法。
$app->get('/files/{path}', function ($path) use ($app) {
if (!file_exists('/base/path/' . $path)) {
$app->abort(404);
}
return $app->sendFile('/base/path/' . $path);
});
也许是这样?
ob_start();
require 'somefile.php';
$contents = ob_get_contents();
ob_end_clean();
return $contents;