skip to Main Content

I have made a controller class that produces a form once the user has inputted the values and submitted the form I have got it to pass one of the form values into the URL.

Issue is it looks like this:

localhost/task/success?0=History

Here is my code for the controller class:

<?php

namespace AppController;

use AppEntityTask;
use AppFormTaskType;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationResponse;

class TaskController extends AbstractController
{
    #[Route('/task', name: 'task')]
    public function taskForm(Request $request): Response
    {
        $task = new Task();

        $form = $this->createForm(TaskType::class, $task);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
        {
            return $this->redirectToRoute('task_success', (array)$task->getTask());
            //return $this->redirectToRoute('task_success'            (array)$request->request->get($task->getTask()));
        }

        return $this->render('Task/new.html.twig', [
            'form' => $form->createView(),
        ]);
    }
    #[Route('/task/success', name:'task_success')]
    public function taskResult(Request $request): Response
    {
        $query = $request->query->get('0');
        return $this->render('Task/test.html.twig',[
            'task' => $query,
        ]);
    }
}

How can I make it so the query isn’t 0= and so that it is say the name of the textfield that being task in this case

2

Answers


  1. You are converting the $task->getTask() to an array, which will do a string to array conversion, and create an array with index 0.

    To fix this, you want to name the parameter.

    public function taskForm(Request $request): Response
    {
    // ...
        return $this->redirectToRoute('task_success', ['task' => $task->getTask()]);
    // ...
    }
    

    You can then grab the query parameter task.

    public function taskResult(Request $request): Response
    {
        $query = $request->query->get('task');
        // ...
    }
    
    Login or Signup to reply.
  2. Sometimes the version of PHP or the browser doesn’t display some information

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search