R
R
ragnar_ok2019-06-24 13:52:49
symfony
ragnar_ok, 2019-06-24 13:52:49

Symfony 4: how to display a template on the main page?

Entered in terminal:

$ php bin/console make:controller DefaultController

Specified in the controller:
// ...
class DefaultController extends Controller
{
    /**
     * @Route("/")
     */
    public function new(Request $request)
    {

        // ...
        return $this->render('default/new.html.twig', [
            'default' => $default->createView(),
        ]);
    }
}

Now instead of: "Welcome to Symfony 4.3.1. Your application is now ready..." (base.html.twig). The new.html.twig template is rendered.
Question: I displayed the generated DefaultController new.html.twig template on the main page. Did I get it right? Was it necessary to use base.html.twig like this? index.php do not touch? Maybe someday you need not to use routing, but just create a page? For example: /public/page.php
// ...
    /**
     * @Route("/")
     */
    public function new(Request $request)
    {

        // ...
        return $this->render('base.html.twig', [
            'default' => $default->createView(),
        ]);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nujabes37, 2019-06-25
@Nujabes37

If you are implementing a JSON API service, then routing is required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question