Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect from embedded controller in Symfony2

Tags:

php

symfony

I have two controllers, say loginAction() and registerAction embedded into the index page (index.html.twig) as so:

// index.html.twig
{% block header %}
{% if app.session.get('loggedin') is null%}
<div class="linear_form_holder">
    {% render "AppBaseBundle:Core:login" %}
</div>
{% endif %}
{% endblock %}

now, in the login controller, i am using this :

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);
}

But, i am getting this error:

An exception has been thrown during the rendering of a template ("Error when rendering "http://localhost/web/app_dev.php/" (Status code is 301).") in AppBaseBundle:Core:index.html.twig at line 6.

How do i do redirection in an embedded controller?

like image 895
kumarharsh Avatar asked Jan 23 '12 11:01

kumarharsh


1 Answers

Forget redirecting from sub-request.

The solution is: 1. Let the form you create in your sub-template post to your parent controller. (form can still be created in different controller, no problem) 2. in parent controller check your request to catch the info coming in from the subcontroller and check if the info of your sub-template form comes in.

eg:

$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
}

For login it is best to add it to your base template, but when you have forms called by AJAX, this is the solution I use.

Good luck

Any questions? Keep me posted.

like image 176
Mathieu Dierckx Avatar answered Sep 17 '22 14:09

Mathieu Dierckx