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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With