尽管在我的控制器 [Symfony] 中定义了路由,但无法重定向到路由
Cannot redirect to route despite having defined the routes in my controller [Symfony]
我的问题是,每当我尝试在我的控制器中使用 "redirectToRoute" 方法时,它永远找不到路由“/group-b”,尽管在控制器中定义了两条路由。这是我收到的错误:
Unable to generate a URL for the named route "/group-b" as such route does not exist.
检查调试路由器后,我发现该路由确实存在,并且当我通过 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”。非常感谢任何帮助。
方法 redirectToRoute
接受路由名称,而不是 uri。
如 Vadim 所述,将代码更改为:
return $this->redirectToRoute("groupB");
我的问题是,每当我尝试在我的控制器中使用 "redirectToRoute" 方法时,它永远找不到路由“/group-b”,尽管在控制器中定义了两条路由。这是我收到的错误:
Unable to generate a URL for the named route "/group-b" as such route does not exist.
检查调试路由器后,我发现该路由确实存在,并且当我通过 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”。非常感谢任何帮助。
方法 redirectToRoute
接受路由名称,而不是 uri。
如 Vadim 所述,将代码更改为:
return $this->redirectToRoute("groupB");