How to get the request parameters in Symfony 2?

后端 未结 16 1521
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条回答
  •  攒了一身酷
    2020-12-02 05:35

    Your options:

    1. Simple:
      • $request->request->get('param') ($_POST['param']) or
      • $request->query->get('param') ($_GET['param'])
    2. Good Symfony forms with all validation, value transormation and form rendering with errors and many other features:
      • http://symfony.com/doc/current/book/forms.html
      • http://symfony.com/doc/current/cookbook/form/index.html
    3. Something in between (see example below)
    setDefaults([
            'email' => '',
            'phone' => '',
        ]);
        $filter = $optionsResolver->resolve($request->query->all());
    
        /** @var CustomerRepository $customerRepository */
        $customerRepository = $this->getDoctrine()->getRepository('AppBundle:Customer');
    
        /** @var Customer[] $customers */
        $customers = $customerRepository->findFilteredCustomers($filter);
    
        return $this->render(':customers:index.html.twig', [
            'customers' => $customers,
            'filter' => $filter,
        ]);
    }
    

    More about OptionsResolver - http://symfony.com/doc/current/components/options_resolver.html

提交回复
热议问题