Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 助手服务中的Symfony重定向不起作用 介绍_Php_Symfony_Redirect_Url Redirection_Symfony4 - Fatal编程技术网

Php 助手服务中的Symfony重定向不起作用 介绍

Php 助手服务中的Symfony重定向不起作用 介绍,php,symfony,redirect,url-redirection,symfony4,Php,Symfony,Redirect,Url Redirection,Symfony4,对于我的个人项目,我正在使用 Symfony v4.2 XAMPP和 Widows 10 Pro 为了不在URL中显示路由参数,我将它们保存在表中。 然后,在控制器中,我检查会话中是否有变量(保持UUID与路由参数相对应) 若我在会话中并没有得到任何变量,那个么它应该重定向到起始页部分,表中的UUID和初始数据都是在这里设置的 重定向逻辑被提取到帮助器服务。为了重定向到工作,有复制的函数redirectorroute和redirect 我通过删除temp文件夹中的php会话变量和浏览器中的P

对于我的个人项目,我正在使用

  • Symfony v4.2
  • XAMPP
  • Widows 10 Pro
为了不在URL中显示路由参数,我将它们保存在表中。 然后,在控制器中,我检查会话中是否有变量(保持UUID与路由参数相对应)

若我在会话中并没有得到任何变量,那个么它应该重定向到起始页部分,表中的UUID和初始数据都是在这里设置的

重定向逻辑被提取到帮助器服务。为了重定向到工作,有复制的函数
redirectorroute
redirect

我通过删除temp文件夹中的php会话变量和浏览器中的PHPSESSID cookie来测试这个功能

问题 prolem是-它不会重定向到secton起始页

如果选择了分支,我可以看出这是正确的,但它“只是停止”并且不执行重定向

代码 问题:
enyone对本案的罪魁祸首有什么看法吗?

嗯,我想您的代码是在服务中,而不是在控制器中? 当控制器发送最终响应时,不能从服务重定向,只能从控制器重定向

您必须从服务返回布尔值,并从控制器重定向:

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
}
在控制器中:

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
}

嗯,我想你的代码在服务中,而不是在控制器中? 当控制器发送最终响应时,不能从服务重定向,只能从控制器重定向

您必须从服务返回布尔值,并从控制器重定向:

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
}
在控制器中:

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
}

您可以尝试将路由器注入到您的服务类中:

use Symfony\Component\Routing\RouterInterface;
类MyService { 私人路由器

public function __construct(RouterInterface $router)
{
    $this->router = $router;
}

public function checkWhereaboutsExist()
{
    // your code ...

    return new RedirectResponse($this->router->generate('section_start'));
}

}

您可以尝试将路由器注入到您的服务类中:

use Symfony\Component\Routing\RouterInterface;
类MyService { 私人路由器

public function __construct(RouterInterface $router)
{
    $this->router = $router;
}

public function checkWhereaboutsExist()
{
    // your code ...

    return new RedirectResponse($this->router->generate('section_start'));
}

}

假设您正确地知道正在使用侦听器,那么您可能应该知道内核请求侦听器可以通过GetResponseEvent数据结构返回响应。不需要控制器。这主意不错,但实现还需要一些工作。谢谢,您的解决方案很有效!但我更喜欢Roberts解决方案,因为它能找到停止的地方并修复我的错误。我确认Cerad评论是处理这个问题的最佳方式;-)假设使用的侦听器是正确的,那么您可能应该知道内核请求侦听器可以通过GetResponseEvent数据结构返回响应。不需要控制器。这主意不错,但实现还需要一些工作。谢谢,您的解决方案很有效!但我更喜欢Roberts解决方案,因为它能找到停止的地方并修复我的错误。我确认Cerad评论是处理这个问题的最佳方式;-)非常感谢。你的解决方案是正确的。我已接近解决方案,但需要新的重定向响应。谢谢!你的解决方案是正确的。我已接近解决方案,但需要新的重定向响应。