php 在 symfony 2 中的控制器内加载文件内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18785524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load file content inside controller in symfony 2
提问by Ladislav M
I have a css file called 'style.css' and I want to load its content inside a controller. I didn't find any relevant solution when reading the official documentation (Cookbook included).
我有一个名为“style.css”的 css 文件,我想将其内容加载到控制器中。在阅读官方文档(包括Cookbook)时,我没有找到任何相关的解决方案。
采纳答案by Paul Andrieux
Symfony2 provides a component called "Finder", check the doc here: http://symfony.com/en/doc/current/components/finder.html
Symfony2 提供了一个名为“Finder”的组件,请在此处查看文档:http: //symfony.com/en/doc/current/components/finder.html
回答by Jay Sheth
I think you would first need to get the path to the CSS directory, using something like:
我认为您首先需要使用以下内容获取 CSS 目录的路径:
$path = $this->get('kernel')->getRootDir() . '/../css' . '/path/to/file.css';
Then, load the CSS file into a string:
然后,将 CSS 文件加载到字符串中:
$css = file_get_contents($path);
Hope that helps!
希望有帮助!
回答by Oscar David
I found a simple solution if you have the path to the file then
如果您有文件的路径,我找到了一个简单的解决方案
$file = new SplFileInfo('/path/to/file.css', '', '');
and the method getContents
does the magic
这个方法很getContents
神奇
$file->getContents();
But you will have to include the class
但你必须包括课程
use Symfony\Component\Finder\SplFileInfo;
is also part of the finder library, but you don't need to search for the file you want to read.
也是 finder 库的一部分,但您不需要搜索要阅读的文件。