2012-05-13 9 views
11

Symfony2와 FOSRestBundle을 사용하여 REST 프레임 워크를 만들려고 노력 중이며 비참하게 실패하고 있습니다.FOSRestBundle을 사용할 수 없습니다.

나는 다음과 같은 짓을 :

namespace Rest\WebServiceBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use FOS\RestBundle\View\View; 


class DefaultController extends Controller 
{ 

    public function indexAction($name) 
    { 


    $view = View::create() 
      ->setStatusCode(200) 
      ->setData($name); 
     return $this->get('fos_rest.view_handler')->handle($view); 


    } 
} 
: 내 컨트롤러에서 내 응용 프로그램에서

[FOSRest] 
    git=git://github.com/FriendsOfSymfony/FOSRest.git 
    target=fos/FOS/Rest 

[FOSRestBundle] 
    git=git://github.com/FriendsOfSymfony/FOSRestBundle.git 
    target=bundles/FOS/RestBundle 

[JMSSerializerBundle] 
    git=git://github.com/schmittjoh/JMSSerializerBundle.git 
    target=bundles/JMS/SerializerBundle 

/config.yml

fos_rest: 
    view: 
     formats: 
      rss: true 
      xml: false 
     templating_formats: 
      html: true 
     force_redirects: 
      html: true 
     failed_validation: HTTP_BAD_REQUEST 
     default_engine: twig 


sensio_framework_extra: 
    view: 
     annotations: false 

: 내 deps 파일에

URL : 012로 이동하면

내가 얻을 :

Unable to find template "". 
500 Internal Server Error - InvalidArgumentException 
2 linked Exceptions: Twig_Error_Loader » Twig_Error_Loader 

문서는 나에게 혼란 보인다 나는 계속할 수 없습니다입니다. 필자가 원하는 것은 컨트롤러에 데이터 배열을 전달하고 JSON 형식을 돌려받을 수 있어야한다는 것입니다. 누군가 도울 수 있습니까?

+4

이 문제가 발생하는 데 문제가 있습니다. 비교적 간단한 작업처럼 보이는 것은 꽤 혼란스러워 보입니다. 그걸 가지고 행운이 있었나요? – greg

답변

17

config.yml 섹션에서 json 형식을 사용하도록 설정하고 다른 형식을 사용하지 않도록 설정하고 경로에 기본값 _format의 기본값을 json으로 설정해야합니다. 예컨대

# app/config/config.yml 
fos_rest: 
    view: 
     formats: 
      json: true 
      rss: false # removing them will also work 
      xml: false 
#....... 

#bundle/routing.yml 
route_name: 
    pattern: /route 
    defaults: { _controller: Bundle:Controller:Method, _format:json } 

또는 컨트롤러는

$view->setFormat('json'); 

는 또한 문서에 주어진 예 링크를 체크 아웃 할 수 있습니다.

+3

그것은 나에게도 효과적 이었지만 배열을 데이터로 사용하는 경우에만 객체를 출력하고 싶습니다. – alex88

+1

최신 버전에서는 작동하지 않습니다. 보다 최근의 답변 : http://stackoverflow.com/a/18035437/842697. 의견은 매우 interesants 있습니다. –

관련 문제