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');
    }  
}
2
  • 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. Commented May 29, 2020 at 17:37
  • Does this answer your question? symfony - download files Commented May 29, 2020 at 19:50

1 Answer 1

3

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');
    }  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.