goulashsoup goulashsoup - 3 months ago 26
PHP Question

How to redirectToRoute() with arguments which are not part of the route?

I have a Controller with two

...Action()
-methods. When I call the route
/newTask
with the route name
newTask
inside the browser I have a form to set a
Task
-object. After submitting via
Submit
-Button I want to redirect to the route
/success
with the route name
task_success
:

class FormController extends Controller {

/**
* @Route("/newTask", name="newTask")
*/
public function newAction(Request $request)
{
// create a task and give it some dummy data for this example
$task = new Task();
$task->setTask('Write a blog post');

$task->setDueDate(new \DateTime('tomorrow'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Here I want to redirect...
return $this->redirectToRoute('task_success', array('task' => $task));
}

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

/**
* @Route("/success", name="task_success")
*/
public function successAction($task){
return $this->render('default/success.html.twig',array('task' => $task));
}

}


As you can see the method
successAction
has a parameter
$task
which I need to to show the values with the
success.html.twig
, but I donĀ“t need
task
as a part of the route (e.g.
\success\{task}
).

So how can I redirect with the argument
$task
without using it inside the route?

Answer

If you want not display the task number in URL, you can pass its ID to the session flashBag before redirect to store it temporally:

$this->addFlash('success_task_id', $task->getId());

return $this->redirectToRoute('task_success');

Then, gets this ID from session in you successAction method:

/**
 * @Route("/success", name="task_success")
 */
public function successAction(){
    $taskId = $this->get('session')->getFlashBag()->get('success_task_id');
    $task = $this->getDoctrine()->getRepository('AppBundle:Task')->find($taskId);

    return $this->render('default/success.html.twig', array('task' => $task));
}

However, if the visibility of the task number is not important, simply use in newAction:

return $this->redirectToRoute('task_success', array('task' => $task->getId()));

This generate one URL like /success?task=1 and gets the number from request query parameter bag in successAction:

public function successAction(Request $request){
    $taskId = $request->query->get('task');

    // ...
}

Another solution using serialize and unserialize functions:

$this->addFlash('success_task', serialize($task));

return $this->redirectToRoute('task_success');

Then, gets the serialized data from session in you successAction method:

/**
 * @Route("/success", name="task_success")
 */
public function successAction(){
    $task = unserialize($this->get('session')->getFlashBag()->get('success_task'));

    return $this->render('default/success.html.twig', array('task' => $task));
}
Comments