2016-10-19 6 views
0

SPA에서 사용하기 위해 API를 설정하려고하지만 브라우저에서 나뭇 가지 템플릿을 볼 수 없지만 단지 $ 데이터 응답이라고 생각합니다.Symfony 3 FOSRestBundle 나뭇 가지 - 렌더링하지 않음

어디에서 템플릿을 지정해야합니까? 정확히 어디에 템플릿을 넣어야합니까?

AppBundle/컨트롤러/DefaultController.php

namespace AppBundle\Controller; 

use Symfony\Component\HttpFoundation\Request; 
use FOS\RestBundle\View\View; 

class DefaultController 
{ 
    /** 
    * @Route("/", name="homepage") 
    * 
    */ 
    public function indexAction(Request $request) 
    { 
     $data = 'string'; 
     $view = View::create(); 
     $view 
      ->setData($data) 
      ->setTemplate("default/index.html.twig"); 

     return $view; 
    } 
} 

config.yml

#Nelmio CORS 
nelmio_cors: 
    defaults: 
     allow_origin: ["%cors_allow_origin%"] 
     allow_methods: ["POST", "PUT", "GET", "DELETE", "OPTIONS"] 
     allow_headers: ["content-type", "authorization"] 
     max_age:  3600 
    paths: 
     '^/': ~ 

# FOS REST Bundle 
fos_rest: 
    body_listener: true 
    format_listener: true 
    param_fetcher_listener: true 
    view: 
     default_engine: twig 
     view_response_listener: 'force' 
     exception_wrapper_handler: null 
     formats: 
      jsonp: true 
      json: true 
      xml: false 
      rss: false 
     templating_formats: 
      html: true 
     mime_types: 
      json: ['application/json', 'application/x-json'] 
      jpg: 'image/jpeg' 
      png: 'image/png' 
     jsonp_handler: ~ 
    routing_loader: 
     default_format: json 
     include_format: false 
    format_listener: 
     rules: 
      - { path: ^/, priorities: [ json, jsonp ], fallback_format: html, prefer_extension: true } 
    exception: 
     enabled: true 
+0

휴식 응답 (일반적으로 JSON, XML 등)을 원하십니까, 아니면 나뭇 가지보기를 원하십니까? – LBA

+0

엔트리 포인트 즉, mydomain.com/defaultController를 설정하고 싶습니다. - Twig 응답과 거기에서 mydomain.com/todo SPA/React/API - JSON 응답 – Dalli

+0

죄송합니다. – LBA

답변

0

당신이 다음

을 시도 할 수 HTML을 제공하기위한 DefaultController를 사용하는 경우
class DefaultController 
{ 
    /** 
    * @Route("/", name="homepage") 
    * 
    */ 
    public function indexAction(Request $request) 
    { 
    $twigData = array('data' => 'string'); 

    return $this->render('BundleName:FolderName:index.html.twig', $twigData); 


    } 
} 
고르 당신이 항상의 nginx를 가질 수 요청하거나 아파치가 당신의 API를 경로에 리디렉션210

그런 다음 또 다른 제안을 또한 {{ data }}

사용보다는 번들을 사용하여 index.html.twig의 데이터 변수를 참조 할 수 있습니다 관련 저장소 루트 폴더

+0

내 코드에 문제가있는 것 같아서 작동하지 않습니다. 응용 프로그램의 구조에 대해 묻고 싶습니다. – Dalli

+0

src 폴더에 아래와 같은 구조를 만들어야합니까? - AppBundle - ApiBundle – Dalli

+0

위의 것을 시도 할 때 어떤 오류가 발생합니까? –

관련 문제