0

我的问题是,每当我尝试在控制器内使用“redirectToRoute”方法时,尽管在控制器内定义了两条路由,但它永远找不到路由“/group-b”。这是我收到的错误:

无法为命名路由“/group-b”生成 URL,因为这样的路由不存在。

检查调试路由器后,我发现路由确实存在,当我通过 URL 栏将路由更改为 group-b ( http://localhost:8000/group-b ) 时,我仍然可以手动找到路由。

这是我的控制器:

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');
    }

}

这是我的调试路由器(显示 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
 -------------------------- -------- -------- ------ -----------------------------------

我无法弄清楚为什么它在定义和存在时不会重定向到路由“/group-b”。任何帮助是极大的赞赏。

4

2 回答 2

1

正如 Vadim 提到的,将代码更改为:

return $this->redirectToRoute("groupB");
于 2018-06-26T15:16:00.470 回答
0

方法redirectToRoute接受路由名称,而不是 uri。

于 2018-06-26T15:14:32.193 回答