class DefaultController extends Controller
{
/**
* Lists all File entities.
*
* @Route("/", name="homepage")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$entities = $em->getRepository('MyFrontendBundle:File')->findAll();
return array('entities' => $entities);
}
/**
* Finds and displays a File entity.
*
* @Route("/download/{filename}", name="file_show")
*/
public function showAction($filename)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('MyFrontendBundle:File')
->findOneByFilename($filename);
if (!$entity) {
throw $this->createNotFoundException('Unable to find File entity.');
}
$response = new Response();
$response->setContent(base64_decode($entity->getContents()));
$response->setStatusCode(200);
$response->headers->set('Content-Type', $entity->getMime());
return $response;
}
}
Listing 29.2. Dwie akcje kontrolera DefaultController
Rozdział 29. Udostępnianie plików binarnych