加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从Symfony2中的嵌入式控制器重定向

发布时间:2020-12-13 18:01:41 所属栏目:PHP教程 来源:网络整理
导读:我有两个控制器,比如loginAction()和registerAction嵌入到索引页面(index.html.twig)中,如下所示: // index.html.twig{% block header %}{% if app.session.get('loggedin') is null%}div class="linear_form_holder" {% render "AppBaseBundle:Core:login"
我有两个控制器,比如loginAction()和registerAction嵌入到索引页面(index.html.twig)中,如下所示:
// index.html.twig
{% block header %}
{% if app.session.get('loggedin') is null%}
<div class="linear_form_holder">
    {% render "AppBaseBundle:Core:login" %}
</div>
{% endif %}
{% endblock %}

现在,在登录控制器中,我使用这个:

public function loginAction(Request $request) {
    if ($password == $record->getPassword()) {
    /* then set the session variables */
        $session->set('loggedin','1');
        $session->set('username',$record->getUsername());
        $session->set('userid',$record->getId());
    /* and grant access to the profile */
        return $this->redirect($this->generateUrl('home'),301);
    }
    else 
        return $this->redirect($this->generateUrl('main_page'),301);
}

但是,我收到此错误:

在呈现模板期间抛出异常(“呈现时出错”http://localhost/web/app_dev.php/“(状态代码为301).”)在AppBaseBundle:Core:index.html.twig at第6行.

如何在嵌入式控制器中进行重定向?

忘记从子请求重定向.

解决方案是:
1.让您在子模板中创建的表单发布到您的父控制器. (表单仍然可以在不同的控制器中创建,没问题)
2.在父控制器中检查您的请求以捕获来自子控制器的信息,并检查您的子模板表单的信息是否进入.

例如:

$myPostInfo = $request->request->all()
if(isset($myPostInfo['acme_userbundle_login'])) {
  die('form has been submitted');
  // here you can do the stuff one would do on a committed form
}

对于登录,最好将其添加到您的基本模板,但是当您有AJAX调用的表单时,这是我使用的解决方案.

祝好运

任何问题?让我发布.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读