帮助程序服务中的 Symfony 重定向不起作用
Symfony redirect in helper service does not work
简介
对于我的个人项目,我正在使用
Symfony v4.2
和
XAMPP
和
Widows 10 Pro
为了不在 URL 中显示路由参数,我将它们保存在 table 中。
然后在控制器中我检查会话中是否有变量(保持与路由参数对应的 UUID)。
如果我在会话中没有得到变量,它应该重定向到部分开始页面,其中设置了 table 中的 UUID 和初始数据。
重定向逻辑被提取到辅助服务。为了重定向到工作有复制函数 redirectToRoute
和 redirect
我通过删除临时文件夹中的 php 会话变量和浏览器中的 PHPSESSID cookie 来测试此功能。
问题
问题是 - 它不会重定向到分区起始页。
我可以看到如果选择分支是正确的,但是 "just stops" 并且不执行重定向。
代码
public function checkWhereaboutsExist()
{
$em = $this->entityManager;
$repo_whereabouts = $em->getRepository(Whereabouts::class);
$whereabouts = $this->session->get('whereabouts');
if (($whereabouts === null) || ($whereabouts === ''))
{
$data = 'whereabouts === '.$whereabouts;
dump($data);
/*
HERE IT STOPS
*/
return $this->redirectToRoute('section_start');
}
else
{
$my_whereabouts = $repo_whereabouts->getWhereabouts($whereabouts);
if (!$my_whereabouts)
{
return $this->redirectToRoute('section_start');
}
}
}
问题
有没有人知道这种情况的罪魁祸首是什么?
嗯嗯,我想你的代码在服务中而不是在你的控制器中?
您不能从服务重定向,而只能从控制器重定向,因为控制器发送最终响应。
您必须 return 来自您的服务的布尔值并从您的控制器重定向:
public function hasToGoToStart()
{
$em = $this->entityManager;
$repo_whereabouts = $em->getRepository(Whereabouts::class);
$whereabouts = $this->session->get('whereabouts');
if (($whereabouts === null) || ($whereabouts === ''))
{
return true;
}
else
{
$my_whereabouts = $repo_whereabouts->getWhereabouts($whereabouts);
if (!$my_whereabouts)
{
return true;
}
}
return false;
}
在你的控制器中:
if ($myService->hasToGoToStart()) {
// redirect
}
您可以尝试将路由器注入您的服务class:
use Symfony\Component\Routing\RouterInterface;
class 我的服务
{
私有 $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function checkWhereaboutsExist()
{
// your code ...
return new RedirectResponse($this->router->generate('section_start'));
}
}
简介
对于我的个人项目,我正在使用
Symfony v4.2
和XAMPP
和Widows 10 Pro
为了不在 URL 中显示路由参数,我将它们保存在 table 中。 然后在控制器中我检查会话中是否有变量(保持与路由参数对应的 UUID)。
如果我在会话中没有得到变量,它应该重定向到部分开始页面,其中设置了 table 中的 UUID 和初始数据。
重定向逻辑被提取到辅助服务。为了重定向到工作有复制函数 redirectToRoute
和 redirect
我通过删除临时文件夹中的 php 会话变量和浏览器中的 PHPSESSID cookie 来测试此功能。
问题
问题是 - 它不会重定向到分区起始页。
我可以看到如果选择分支是正确的,但是 "just stops" 并且不执行重定向。
代码
public function checkWhereaboutsExist()
{
$em = $this->entityManager;
$repo_whereabouts = $em->getRepository(Whereabouts::class);
$whereabouts = $this->session->get('whereabouts');
if (($whereabouts === null) || ($whereabouts === ''))
{
$data = 'whereabouts === '.$whereabouts;
dump($data);
/*
HERE IT STOPS
*/
return $this->redirectToRoute('section_start');
}
else
{
$my_whereabouts = $repo_whereabouts->getWhereabouts($whereabouts);
if (!$my_whereabouts)
{
return $this->redirectToRoute('section_start');
}
}
}
问题
有没有人知道这种情况的罪魁祸首是什么?
嗯嗯,我想你的代码在服务中而不是在你的控制器中? 您不能从服务重定向,而只能从控制器重定向,因为控制器发送最终响应。
您必须 return 来自您的服务的布尔值并从您的控制器重定向:
public function hasToGoToStart()
{
$em = $this->entityManager;
$repo_whereabouts = $em->getRepository(Whereabouts::class);
$whereabouts = $this->session->get('whereabouts');
if (($whereabouts === null) || ($whereabouts === ''))
{
return true;
}
else
{
$my_whereabouts = $repo_whereabouts->getWhereabouts($whereabouts);
if (!$my_whereabouts)
{
return true;
}
}
return false;
}
在你的控制器中:
if ($myService->hasToGoToStart()) {
// redirect
}
您可以尝试将路由器注入您的服务class:
use Symfony\Component\Routing\RouterInterface;
class 我的服务 { 私有 $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function checkWhereaboutsExist()
{
// your code ...
return new RedirectResponse($this->router->generate('section_start'));
}
}