FOSUserBundle: enabling the login form and selecting its template

I want to insert the login form in FOSUserBundle into my template, for example this:

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

but do not display the template that is called in the loginAction () code initially.

I thought that I would find a useful way of passing the pattern. I want to make it as a parameter like max in this example:

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

Is this possible in symfony2? If not..

Should I create another action for my package with the same code inside the login? or should you change the source code of the login and record management structure?

 if(currentPage == 'home') renderResponse('template1') else renderResponse('template2') 
+8
symfony
source share
4 answers

You can create a partial one to save your regular 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 add your new partial :)

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

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

Then just add a hidden input field to your form:

  <input type="hidden" name="_csrf_token" value="{{ fos_csrf_provider.generateCsrfToken('authenticate') }}" /> 
+33
source share

You must indicate that you are calling the controller

 {{ render(controller('FOSUserBundle:Security:login')) }} 
+10
source share

Since the part where the error was included was not answered here, and I had to delve into the FOSUserBundle code, let me also add to this post how to add error messages to the loginPartial.html.twig form:

See the file / vendor / friendsofsymfony / user-bundle / Controller / SecurityController.php

This controller has a loginAction that creates an error. All you have to do in the controller that you use to render loginPartial is to add the following code to this controller.

 $session = $request->getSession(); // get the error if any (works with forward and redirect -- see below) if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { $error = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR); } elseif (null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { $error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR); $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR); } else { $error = null; } return $this->render('YourDemoBundle:Default:index.html.twig', ['error' => $error]); 

In this case, you simply use the same code in your controller to generate any login error messages, i.e. wrong password, etc.

Now in the branch that shows the form, simply add the following: {% include 'YourDemoBundle: Security: loginPartial.html.twig'%}

And in the loginPartial.html.twig file, enter the following:

 {% block fos_user_content %} {% if error %} <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div> {% endif %}`Let the rest of the form be as is....and then endblock ofcourse..{% endblock fos_user_content %}` 

It should do it

+1
source share

represents one “colon” ​​between each part

 {% render "FOSUserBundle:Security:login" %} 
0
source share

All Articles