Render Template without a custom Controller – Symfony

In any MVC framework when you want to show any page you would normally create a controller and render template from within the controller.

Laravel Framework

class BooksController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $books = Book::all();
        return view('books.index', compact('books'));
    }
}

Symfony Framework

class BooksController extends AbstractController {

	/**
	 * @Route("/")
	 *
	 * @return Response
	 */
	public function index() {
             $books = $this->getDoctrine()->getRepository(Books::class);
             return $this->render("books/index.html.twig", [
			'books' => $liveAlarms,
			]
		);
       }
}

But in Symfony, if you are rendering a simple template, for instance, static content which doesn’t need any data from Controller you can still render it without creating a Controller by using built-in TemplateController.

# config/routes.yaml
acme_privacy:
    path:         /privacy
    controller:   Symfony\Bundle\FrameworkBundle\Controller\TemplateController
    defaults:
        template: static/privacy.html.twig
    methods: GET

Leave a Reply

Your email address will not be published. Required fields are marked *