2016-07-21 2 views
4

심포니 (FOSRestBundle 사용)로 API를 만드는 법을 배우고 있습니다. 나는 프랑스어 자습서를 따라 가고있다. 분명히 먼저 코드를 직접 작성하려고하지만 복사/붙여 넣기를하더라도 적절한 경로 (rest-api.local/places)로 GET 요청을하면 빈 JSON 배열을 유지합니다.symfony/FOSRestBundle : 빈 JSON 응답 (심포니 구현 시리얼 라이저 사용)

public function getPlacesAction(Request $request) 
{ 
    $places = $this->get('doctrine.orm.entity_manager') 
      ->getRepository('AppBundle:Place') 
      ->findAll(); 
    /* @var $places Place[] */ 

    $formatted = []; 
    foreach ($places as $place) { 
     $formatted[] = [ 
      'id' => $place->getId(), 
      'name' => $place->getName(), 
      'address' => $place->getAddress(), 
     ]; 
    } 

    return new JsonResponse($formatted); 
} 

을하지만 그때 나는 (config.yml에서) fost 나머지의 뷰 핸들러를 사용하여, 직접 $ 장소를 직렬화하려고 : 나는 PHP 배열의 코드를 "포맷"경우

코드는 작동 확인

fos_rest: 
routing_loader: 
    include_format: false 
view: 
    view_response_listener: true 
format_listener: 
    rules: 
     - { path: '^/', priorities: ['json'], fallback_format: 'json' } 

내 컨트롤러의 함수를 다음 코드로 변경하면 "{[], [], []}"사이에 아무 것도없는 JSON 응답이 나타납니다 (내 DB에는 3 개의 항목이 있습니다) :

public function getPlacesAction(Request $request) 
{ 
    $places = $this->get('doctrine.orm.entity_manager') 
      ->getRepository('AppBundle:Place') 
      ->findAll(); 
    /* @var $places Place[] */ 

    return $places; 
} 

그것은 내 첫 번째 게시물 stackoverflow, 그래서 내 질문은 분명 희망, 좋은 하루 되세요. 내 app/config/services.yml에서

+0

어떤 시리얼 라이저를 사용하고 있습니까? 일반적으로 serializer는 [JMSSerializerBundle] (http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies) – Jeet

답변

1

나는이 추가되었습니다 : 그것은 작동하지만 갑자기 나는 응답에 좋은 JSON있어 왜

services: 
    object_normalizer: 
     class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer 
     # Important! Tag this service or it wouldn't work 
     tags: 
      - { name: serializer.normalizer } 

나는 아직도 얻을 수 없습니다.

+1

처럼 엔티티 수준에서 추가 노출이 발생했습니다. 뿐만 아니라 jms 시리얼 라이저 사용하기 – damioune123

관련 문제