How to get the request parameters in Symfony 2?

后端 未结 16 1515
Happy的楠姐
Happy的楠姐 2020-12-02 05:02

I am very new to symfony. In other languages like java and others I can use request.getParameter(\'parmeter name\') to get the value.

Is there anything

相关标签:
16条回答
  • public function indexAction(Request $request)
    {
       $data = $request->get('corresponding_arg');
       // this also works
       $data1 = $request->query->get('corresponding_arg1');
    }
    
    0 讨论(0)
  • 2020-12-02 05:40

    You can Use The following code to get your form field values

    use Symfony\Component\HttpFoundation\Request;
    
    public function updateAction(Request $request)
    {
        // retrieve GET and POST variables respectively
        $request->query->get('foo');
        $request->request->get('bar', 'default value if bar does not exist');
    }
    

    Or You can also get all the form values as array by using

    $request->request->all()
    
    0 讨论(0)
  • 2020-12-02 05:40

    You can do it this:

    $clientName = $request->request->get('appbundle_client')['clientName'];
    

    Sometimes, when the attributes are protected, you can not have access to get the value for the common method of access:

    (POST)

     $clientName = $request->request->get('clientName');
    

    (GET)

    $clientName = $request->query->get('clientName');
    

    (GENERIC)

    $clientName = $request->get('clientName');
    
    0 讨论(0)
  • 2020-12-02 05:42

    I do it even simpler:

    use Symfony\Component\HttpFoundation\Request;
    
    public function updateAction(Request $request)
    {
        $foo = $request->get('foo');
        $bar = $request->get('bar');
    }
    

    Another option is to introduce your parameters into your action function definition:

    use Symfony\Component\HttpFoundation\Request;
    
    public function updateAction(Request $request, $foo, $bar)
    {
        echo $foo;
        echo $bar;
    }
    

    which, then assumes that you defined {foo} and {bar} as part of your URL pattern in your routing.yml file:

    acme_myurl:
        pattern:  /acme/news/{foo}/{bar}
        defaults: { _controller: AcmeBundle:Default:getnews }
    
    0 讨论(0)
提交回复
热议问题