Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file content inside controller in symfony 2

Tags:

php

symfony

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

like image 499
Ladislav M Avatar asked Sep 13 '13 11:09

Ladislav M


3 Answers

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!

like image 197
Jay Sheth Avatar answered Oct 23 '22 17:10

Jay Sheth


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

like image 35
Paul Andrieux Avatar answered Oct 23 '22 18:10

Paul Andrieux


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.

like image 4
Oscar Gallardo Avatar answered Oct 23 '22 18:10

Oscar Gallardo