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?
If you don't want display the task number in URL, you can pass its ID to the session flashBag before redirect and store it temporally:
$this->addFlash('success_task_id', $task->getId());
return $this->redirectToRoute('task_success');
Then, get 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 it in newAction:
return $this->redirectToRoute('task_success', array('task' => $task->getId()));
This generate one URL like /success?task=1 and get the number from request query parameter bag in successAction:
public function successAction(Request $request){
    $taskId = $request->query->get('task');
    // ...
}
Another solution by using serialize and unserialize functions:
$this->addFlash('success_task', serialize($task));
return $this->redirectToRoute('task_success');
Then, get 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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With