2012-05-13 10 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

formatsconfig.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です。 –

関連する問題