0

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.

StevenG1
  • 55
  • 1
  • 9

2 Answers2

1

As Vadim mentioned, change the code to:

return $this->redirectToRoute("groupB");
gvf
  • 1,039
  • 7
  • 6
  • Having changed this to the name of the route it has removed my error, however, it does not load the blank page that the route should be on. It remains on the page for groupA and the url never changes to the new route either it remains on /group-a. – StevenG1 Jun 26 '18 at 15:33
  • I've updated the code, a `return` was missing on your code too, please try now. – gvf Jun 26 '18 at 15:37
0

Method redirectToRoute accepts route name, not uri.

Vadim Ashikhman
  • 9,851
  • 1
  • 35
  • 39