Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle: embedding the login form and choosing its template

Tags:

symfony

i want to insert the login form of FOSUserBundle in my template like this:

<div id="sidebar">
     {% render "FOSUserBundle::Security::login" %}
</div> 

but not to render the template that is been calling in the code of loginAction() originally.

I have thought I'd find useful the possibility of passing the template I want to render as a parameter as 'max' in this example:

<div id="sidebar">
    {% render "AcmeArticleBundle:Article:recentArticles" with {'max':
3} %}
</div>

Is that possible in symfony2? If not..

should I create another action for my bundle with the same code inside of loginAction? or should I modify the original loginAction code and write control structures?

if(currentPage == 'home')
 renderResponse('template1')
else
renderResponse('template2')
like image 483
ziiweb Avatar asked Nov 07 '11 17:11

ziiweb


2 Answers

You can create a partial to store your plain HTML form, for example this simple one:

<form action="{{ path("fos_user_security_check") }}" method="post">
  <label>Email</label>
  <input type="text" name="_username" required="required" />

  <label>Password</label>
  <input type="password" name="_password" required="required" />

  <button type="submit">Submit</button>
</form>

Then in your layout just include your brand new partial :)

If you need the csrf token (which is highly recommended), just define the provider as a Twig global variable:

twig:
    globals:
        fos_csrf_provider: "@form.csrf_provider"

Then just add the hidden input field in your form:

  <input type="hidden" name="_csrf_token" value="{{ fos_csrf_provider.generateCsrfToken('authenticate') }}" />
like image 75
David Morales Avatar answered Sep 28 '22 10:09

David Morales


You have to specify that you are calling a controller

{{ render(controller('FOSUserBundle:Security:login')) }}
like image 42
Tom Tom Avatar answered Sep 28 '22 10:09

Tom Tom