【问题标题】:How to redirect other URL in Symfony3 with parameter如何使用参数重定向 Symfony3 中的其他 URL
【发布时间】:2016-04-29 06:18:24
【问题描述】:

我无法使用参数重定向其他 URL。在下面的代码中,它会在 $resultFlg 等于 True 时显示错误消息。

消息指出“无法为命名路由“snsForm”生成 URL,因为这样的路由不存在”。我不明白原因和解决方案。请使用 routing.yml 作为参考。那么,Symfony 版本是 3.0.4-DEV。

src/AppBundle/Controller/UserController.php

/**
 * @Route("/form", name="userForm")
 */
public function newAction(Request $request)
{
     ........
     $resultFlg = $this->existedUserCheck($userId, $password);

     if($resultFlg){
     // failed
         return $this->redirect($this->generateUrl('snsForm'));

     }else{
         $loginResultMessage = 'No registered User Id: ' . $userId;

     }
     .......
}

/**
 * @Route("/snsForm", name="regsiterSnsRoute")
 */
public function registerSnsInfoAction(Request $request)
{
    .......
}

app/config/routing.yml

app:
resource: "@AppBundle/Controller/"
type:     annotation

【问题讨论】:

    标签: php symfony-forms symfony


    【解决方案1】:

    您需要指向路由的名称而不是 url。在您的情况下,它是“regsiterSnsRoute”

    return $this->redirect($this->generateUrl('regsiterSnsRoute'));
    

    你也可以将该行缩短为

    return $this->redirectToRoute('regsiterSnsRoute');
    

    【讨论】:

    • 感谢您的回答,重定向功能正常工作。我以为我必须输入 URL 路径,因为方法名称是 generateUrl。
    猜你喜欢
    • 2018-08-24
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2016-11-25
    • 2015-06-16
    • 2016-02-29
    • 1970-01-01
    相关资源
    最近更新 更多