Search code examples
symfonymime-types

Somfony 2 set header content mime-types automatically


I have a question about Symfony 2. I would like to know how if there is a function implemented in Symfony 2 who return the content mime-type ?

Why mime-type cause trouble ? i have some file and i dont want everyone access to it then i made a methode who check if you have the right to access to this ressource.

chdir("Directory/".$nameoftheressource);
$file = file_get_contents($nameoftheressource);/**/
$namearray=explode(".", $nameoftheressource);
$extension=end($namearray);

$returnFile= new Response();
$returnFile->setContent($file);
if($extension == "css" )
{ $returnFile->headers->set('Content-Type', 'text/css');
  return $returnFile;}

Thanks to you xabbuh this is near to work perfectly and as u said saved lot of time now the code look like

EDIT

    use Symfony\Component\HttpFoundation\BinaryFileResponse;
    //some code
    return new BinaryFileResponse('/Directory/'.$nameoftheressource);

But now it does display the css file but propose me to download it bt i would like to display it as a css normal css file


Solution

  • You can save a lot of code by using the BinaryFileResponse class which among other things automatically adds the right content type header.