1

I'm new to Symfony and am trying to understand that controller response function. I just want to return a simple HTML file home.html, that at the moment just has a Hello World line in it.

How do I return the file as a response? As it stands with the code below it's just returning the string 'home.html'.

Many thanks.

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;


class MainController 
{
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return new Response('home.html');
    }  
}
  • Just use new Response(file_get_contents('home.html') though you may have to specify the directory of home.html as well. And then learn the Symfony way. – Cerad May 29 '20 at 17:37
  • Does this answer your question? [symfony - download files](https://stackoverflow.com/questions/39603052/symfony-download-files) – Nico Haase May 29 '20 at 19:50

1 Answers1

2

Because you need to extend your controller with AbstractController and use the render method to generate the view from html.

class MainController extends AbstractController
{
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return $this->render('home.html');
    }  
}
Mike
  • 63
  • 5