sajad torkamani

Main request

The main request is the original HTTP request that initiated the current HTTP cycle – the one that came from the browser.

You can obtain the main request using:

$requestStack->getMainRequest()

Current request

The current request is the request currently being handled. This can be the main request if it’s the same as the main request or a sub-request.

For example, a sub-request can be initiated with code like this:

// src/Controller/NewsController.php

#[Route('/news')]
public function newsPage(): Response
{
    return $this->render('news.html.twig');
}

#[Route('/sidebar')]
public function sidebar(): Response
{
    return new Response('<p>This is the sidebar</p>');
}

Assuming your news.html.twig has this:

<h1>News Page</h1>
{{ render(controller('App\\Controller\\NewsController::sidebar')) }}

In this case, Symfony will:

  • Handle the main request to the /news route.
  • See render(controller('App\\Controller\\NewsController::sidebar')) in news.html.twig and create a sub-request to /sidebar.
  • Process NewsController::sidebar() as a sub request.
  • Embed its result in the final response.

You can obtain the current request using:

$requestStack->getCurrentRequest()
Tagged: Symfony