14

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).

1
  • 4
    Have you tried php 'file_get_contents' ? Commented Sep 13, 2013 at 11:47

3 Answers 3

23

I think you would first need to get the path to the CSS directory, using something like:

$path = $this->get('kernel')->getRootDir() . '/../css' . '/path/to/file.css';

Then, load the CSS file into a string:

$css = file_get_contents($path);

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

13

Symfony2 provides a component called "Finder", check the doc

Comments

4

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

$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.

1 Comment

It should be the correct answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.