Php 我无法在重定向期间更改新页面的URL

Php 我无法在重定向期间更改新页面的URL,php,symfony,redirect,routes,Php,Symfony,Redirect,Routes,在symfony 3.4上,我想从控制器调用另一个函数。被调用的函数必须重定向到新页面。我想修改页面的URL 然而,使用我使用的方法,页面变化很好,但URL变化不大 计划如下: 控制器=>函数=>显示登录表单(细枝) 登录表单=>验证(POST方法)=>控制器=>函数=>调用其他函数=>显示其他页面(以及修改URL) URL没有改变,我不明白 这是我的密码: 控制器: <?php namespace Site\PagesBundle\Controller; use Site\Pages

在symfony 3.4上,我想从控制器调用另一个函数。被调用的函数必须重定向到新页面。我想修改页面的URL

然而,使用我使用的方法,页面变化很好,但URL变化不大

计划如下:

  • 控制器=>函数=>显示登录表单(细枝)
  • 登录表单=>验证(POST方法)=>控制器=>函数=>调用其他函数=>显示其他页面(以及修改URL)
  • URL没有改变,我不明白

    这是我的密码:

    控制器:

    <?php
    
    namespace Site\PagesBundle\Controller;
    
    use Site\PagesBundle\Entity\User;
    use Site\PagesBundle\Entity\Information;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    
    /**
     * Information controller.
     *
     * @Route("accueil")
     */
    class DefaultController extends Controller
    {
    
        /**
         * Accueil
         *
         * @Route("/", name="connexion_index")
         * @Method({"GET", "POST"})
         */
        public function indexAction(Request $request)
        {
    
            $em = $this->getDoctrine()->getManager(); //Récupération du manager
    
            $listeInfos = $em->getRepository('PagesBundle:Information')->getInformationsZone("Zone 1"); //Récupération d'une liste d'informations
    
            $user = new User(); //Initialisation de l'objet User
            $form = $this->createForm('Site\PagesBundle\Form\ConnexionType', $user); //Formulaire de création
            $form->handleRequest($request);
    
            //Traitement si le formulaire est soumis ( Ajout du package dans la BDD )
            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($user);
    
                $valide = $this->getDoctrine()->getManager()->getRepository('PagesBundle:User')->authentifier($user->getIdentifiant(),$user->getPassword());
                dump($valide);
    
                if($valide == 1)
                {
                    return $this->accueil($request);
                }
    
                else
                {
                    return $this->render('@Pages/Default/connexion.html.twig',array(
                        'user' => $user,
                        'form' => $form->createView(),
                        'listeInfos' => $listeInfos,
                    ));
                }
    
                // On ajoute un package, donc on offre un téléchargement supplémentaire aux utilisateurs concernés
                $this->getDoctrine()->getManager()->getRepository('PagesBundle:User')->updateNbDDLAll("inc"); 
    
                //return $this->redirectToRoute('paquets_index'); // Redirection page de gestion de packages
            }
    
    
    
            return $this->render('@Pages/Default/connexion.html.twig',array(
                'user' => $user,
                'form' => $form->createView(),
                'listeInfos' => $listeInfos,
            ));
        }
    
        /**
         * @Route("/index", name="accueil")
         * @Method("GET")
         */
        public function accueil(Request $request)
        {
            $em = $this->getDoctrine()->getManager(); //Récupération du manager
    
            $listeInfos = $em->getRepository('PagesBundle:Information')->getInformationsZone("Zone 2"); //Récupération d'une liste d'informations
    
            return $this->render('@Pages/Default/accueil.html.twig',array(
                'listeInfos' => $listeInfos,
            ));
        }
    }
    
    if($valide == 1)
            {
                return $this->accueil($request);
            }
    


    感谢您的帮助

    您没有重定向到其他路径,只返回控制器内的
    视图
    索引
    。由于当前url已与此
    操作相对应
    因此不会对url进行任何更改。如果你想导航到另一条路径,你必须告诉Symfony这样做

    if($valide == 1)
    {
        return $this->redirectToRoute('accueil');
    }
    
    然后,您的
    accueil
    功能也必须是
    操作

    /**
     * @Route("/index", name="accueil")
     * @Method("GET")
     */
    public function accueilAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager(); //Récupération du manager
    
        $listeInfos = $em->getRepository('PagesBundle:Information')->getInformationsZone("Zone 2"); //Récupération d'une liste d'informations
    
        return $this->render('@Pages/Default/accueil.html.twig',array(
                'listeInfos' => $listeInfos,
            ));
    }
    

    谢谢你的回答!哦,是的,我现在更明白了,我完全忘记了“行动”的概念。此外,在什么情况下,我应该在我的方法末尾添加“操作”?非常感谢。仅当方法对应于url中的路径时。更多信息可在此处找到:
    /**
     * @Route("/index", name="accueil")
     * @Method("GET")
     */
    public function accueilAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager(); //Récupération du manager
    
        $listeInfos = $em->getRepository('PagesBundle:Information')->getInformationsZone("Zone 2"); //Récupération d'une liste d'informations
    
        return $this->render('@Pages/Default/accueil.html.twig',array(
                'listeInfos' => $listeInfos,
            ));
    }