12

I have this view:

//login.html.twig

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>MY APP</title>
</head>

<body>
    <form action="{{ path('conection') }}" method="post" name="formulario_login">
        <label for="username">User:</label>
        <input type="text" id="username" name="_username" value="{{ last_username|default('') }}" />
        <br />
        <label for="password">Password:</label>
        <input type="password" id="password" name="_password" />
        <br />
        <input type="checkbox" id="remember_me" name="_remember_me"/>
        <label for="remember_me">Remember me</label>
        <br />
        <input type="submit" name="login" value="Login" />
    </form>
</body>
</html>

In my routing file:

conection:
pattern:  /conection
defaults: { _controller: UserBundle:Default:conection}

And my controller

<?php

namespace myApp\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    //some other action functions

    public function conectionAction(){

    }
}

My question is how do I get in the controller the values of user and password fields to work with them?I have search but I don´t see a clear solution.

1

2 Answers 2

17

Try this, by way of example and using Symfony2.8

View:

  <form action="{{ path('connection') }}" method="post" name="formulario_login">
        <label for="username">User:</label>
        <input type="text" id="username" name="_username" value="" />
        <br />
        <label for="password">Password:</label>
        <input type="password" id="password" name="_password" />
        <br />
        <input type="checkbox" id="remember_me" name="_remember_me"/>
        <label for="remember_me">Remember me</label>
        <br />
        <input type="submit" name="login" value="Login" />
    </form>

Controller:

public function connectionAction(Request $request) {
    $username = $request->request->get('_username');
    $password = $request->request->get('_password');
     ....
}

Other way to get parameters, as you can see the naming is not all that intuitive:

 // $_GET parameters
$request->query->get('name');

// $_POST parameters
$request->request->get('name');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks,now I get my variables in the controller, as you say,the query->get() method needs the name of the field,not the id.
Yes @SensacionRC, in the example you will get the name of the fields "_username" and "_password", not the ID, other way and best practice to build form is symfony.com/doc/2.8/forms.html
5

You should pass the Request object to the action method then deal with it:

public function conectionAction(Request $request){
    if ($request->getMethod() == Request::METHOD_POST){
        $user = $request->request->get('user');
        $password = $request->request->get('password');
    }
}

However I suggest you to use the Symfony Form Component to dial with this situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.