It's been quite a long time since I asked this question, and as can be seen from the comments, or if you just read the code, the design was horribly flawed. I was just starting with Symfony2, and the MVC architecture in general, so it was a lot later I realized why I was never going to receive an answer for this question :P
Since then, I've moved on to a lot of different frameworks and completely ditched the convoluted ways of Symfony to embrace node, backbone, angularjs and whatnot, and life is a hell lot simpler.
Anyways, since this question still seems to be gathering some views and stray upvotes from (i am guessing) started in Symfony, here is the fixed code, and some reasoning (I've pulled this from my GIT repo, and this was still a long time ago, i.e., teeth-cutting phase, so I'm not very sure if this is a good way, but whatever)
In your controller
public function indexAction() {
$session = $this->getRequest()->getSession();
if($session->get('loggedin') != null ) {
return $this->redirect($this->generateUrl('home'),301);
} else {
$loginData = array();
$loginForm = $this->createFormBuilder($loginData)
->add('email', 'email')
->add('password', 'password')
->getform();
return $this->render('AppBundle:Core:index.html.twig',
array('loginForm' => $loginForm->createView()) );
}
}
In your view
{% extends '::basetemplate.html.twig' %}
{% block title %}PAGE TITLE{% endblock %}
{% block header %}
{% if app.session.get('loggedin') is null %}
<div class="linear_form_holder">
{% include "AppBundle:Core:login.html.twig" with {'form':loginForm} %}
</div>
{% endif %}
{% endblock %}
Things to take away:
Don't make recursive calls like loginAction
redirecting to main-page
, which calls loginAction
again, like in my question: Not only is the code in my question flawed, but excessive recursion will kill the server anyways, so you might not get a clue what's wrong. Try to keep it simpler.
Abstract out the login and registration and other forms away, and don't use templates for them. Bind them to a model, and use formBuilder for generating the forms.
Extra advice: If you can, switch to newer technologies such as nodejs or maybe Go (I haven't tried it yet). Initially the investment in learning is a lot, but you'll reap its benefits over time. Feel free to disregard this extra advice.
Have fun
:)