Bango
Bango

Reputation: 1001

Passing a regular parameter through the generateUrl function in Symfony2

I'm redirecting to the entity_show route through the createAction. In the create logic, I'm assigning a non-entity variable ($credited) to 'credited' or 'not credited', which I'm then trying to pass to the showAction (entity_show route) through the return call of

this->redirect(generateUrl(entity_show, array('id' => $entity->getId(), $credited)));

I've tried this several ways,

array(..., $credited)
array(..., 'credited' => $credited)
array(..., true/false)

but none of these values are being correctly passed into the showAction, even though I have the parameter defined.

public function showAction($id, $credited = 'credited') { ... }

(I need the default value in case I call showAction from a place other than createAction, where $credited is undefined)

The problem is that in showAction, $credited always falls back to its default value, even when in createAction the value passed was 'not credited', meaning the parameter is not getting passed to showAction at all like I intend it to.

Is it possible to pass a variable through generateUrl() as a normal function parameter instead of something inherently tied to the URL?

Ideally, if I can get my $credited value passed from createAction into showAction, then in showAction I can return $credited to the twig view, where I can use some twig logic to display custom text depending on what value $credited holds.

return array(
    'entity'      => $entity,
    'credited'    => $credited,
    'delete_form' => $deleteForm->createView()
);

Upvotes: 0

Views: 1173

Answers (1)

Cerad
Cerad

Reputation: 48865

It is simple array stuff:

this->redirect(generateUrl('entity_show', array(
    'id' => $entity->getId(), 
    'credited' => $credited
)));

I am assuming of course that entity_show route has id and credited defined.

Updated in response to the first comment.

Try: echo generateUrl('entity_show', array( 'id' => $entity->getId(), 'credited' => $credited )); die();

I suspect that your entity_show route is not correctly defined. Consider updating your question with the route definition as well as the results of the above test. Actually, your browser should be showing the redirected url as well.

Upvotes: 1

Related Questions