Return a JSON array from a Controller in Symfony

后端 未结 7 946
一个人的身影
一个人的身影 2020-12-17 14:30

I am trying return a JSON response from a controller in Symfony 2. Form example, in Spring MVC I can get a JSON response with @ResponseBody annotattion. I want get a JSON re

相关标签:
7条回答
  • 2020-12-17 15:03

    I suggest the following solution:

    /**
         * @Route(
         *      "/drop/getCategory/",
         *      name="getCategory"
         * )
         * @Method("GET")
         */
        public function getAllCategoryAction() {
            $em = $this->getDoctrine()->getManager();
            $query = $em->createQuery(
                'SELECT c
                FROM AppBundle:Categoria c'
            );
            $categorias = $query->getArrayResult();
    
    
            return new Response(json_encode($categorias), 200);
    }
    
    0 讨论(0)
提交回复
热议问题