Symfony get form data in controller

前端 未结 2 2045
眼角桃花
眼角桃花 2021-01-17 22:01

I have this view:

//login.html.twig





    

        
相关标签:
2条回答
  • 2021-01-17 22:20

    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.

    0 讨论(0)
  • 2021-01-17 22:22

    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');
    
    0 讨论(0)
提交回复
热议问题