phpsymfonysymfony-formssymfony-flex

Cannot redirect to route despite having defined the routes in my controller [Symfony]


My problem is that whenever I try to use the "redirectToRoute" method inside my controller it never finds the route "/group-b" despite both routes being defined inside the controller. This is the error I receive:

Unable to generate a URL for the named route "/group-b" as such route does not exist.

Having checked the debug router I have found the route does exist and I can still manually find the route when I change the route via the URL bar to group-b (http://localhost:8000/group-b).

Here is my controller:

use App\Entity\GroupATask;
use App\Form\GroupAType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class GroupStageController extends AbstractController
{
    /**
     * @Route("/group-a", name="groupA")
     */
    public function GroupA(Request $request, EntityManagerInterface $entityManager)
    {
        $groupATask = new GroupATask();
        $groupAForm = $this->createForm(GroupAType::class, $groupATask);

        $groupAForm->handleRequest($request);

        if($groupAForm->isSubmitted() && $groupAForm->isValid()){

        $entityManager->persist($groupATask);

        $entityManager->flush();

        $this->redirectToRoute("/group-b");
    }

        return $this->render('group_stage/groupA.html.twig', [
            "group_a_form" => $groupAForm->createView()
        ]);
}

    /**
     * @Route("/group-b", name="groupB")
     */
    public function GroupB()
    {
        return $this->render('group_stage/groupB.html.twig');
    }

}

Here is my debug router (shows both routes for group-a + group-b

-------------------------- -------- -------- ------ ----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  groupA                     ANY      ANY      ANY    /group-a
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
  groupB                     ANY      ANY      ANY    /group-b
 -------------------------- -------- -------- ------ -----------------------------------

I cannot work out why it will not redirect to the route "/group-b" when it is defined and exists. Any help is greatly appreciated.


Solution

  • As Vadim mentioned, change the code to:

    return $this->redirectToRoute("groupB");