Can redirectToRoute() have arguments like render()?
You might expect that passing variables to a redirect works exactly the same way as passing them to a render call. If you can pass an entity to render() and access its properties in Twig, you naturally assume redirectToRoute() accepts the same parameters so you can pass data along with the URL. However, the behavior is fundamentally different, and understanding why requires looking at what happens after the function returns.
When you use $this->render(), Symfony builds a Response object containing both the HTML content and the template variables. The controller halts execution there, passing that specific response to the kernel. Any variables passed to render() become part of the context available to the Twig engine immediately. The template receives this data as if it were local scope.
redirectToRoute() operates differently because its primary job is navigation, not rendering. It generates a URL and sets the HTTP redirect header on the Response object before returning. Crucially, in older versions of Symfony (such as 2.7 mentioned in the source material), redirectToRoute() does not automatically populate the template context with the arguments you pass to it. The array you provide is used solely for URL generation via the router.
When you execute code like:
return $this->redirectToRoute('frontend_carrodecompras', array(
'entity' => $entity,
));
The framework uses the entity key to construct the query string or path segment for the route named frontend_carrodecompras. If that route expects a parameter in the URL (e.g., /car/123), Symfony extracts that value and appends it to the URL. It does not, however, place $entity into the Twig context for the target template.
This leads to the error you encountered: "Variable 'entity' does not exist." Even if you echo the variable before redirecting to prove it exists in memory, the redirect triggers a new request cycle (or effectively simulates one). The target template is rendered in response to that redirection. Since the original controller's context was cleared or bypassed during the redirect logic, the Twig engine for the destination page has no knowledge of the variables passed to the redirect function.
To achieve your goal of accessing an entity inside a different template after a redirect, you must explicitly pass the data as a flash message. This is a design pattern specific to Symfony's session-based state management between requests. You can store the entity in the session and retrieve it in the destination controller or directly in Twig if configured, though using Flash messages is the idiomatic approach for transient data transfer.
Here is how you modify your code in Symfony 2.7:
// Store the entity in the session
$this->get('session')->getFlashBag()->set('entity', $entity);
return $this->redirectToRoute('frontend_carrodecompras');
In your Twig template, you would then retrieve it:
{% set entity = app.session.flash.get('entity') %}
{{ entity.nombre }}
While this adds an extra step compared to render(), it is the correct way to handle data transfer across redirects. Relying on implicit context passing in redirects breaks the separation of concerns between routing logic and view rendering. If you are using a newer version of Symfony (3.x or later), the API for flash messages has evolved, but the core principle remains: redirectToRoute() generates a URL; it does not forward controller variables to the next request's template context automatically.