簡體   English   中英

Symfony 3 FOSRestBundle樹枝-不渲染

[英]Symfony 3 FOSRestBundle Twig - not render

我正在嘗試設置API以在SPA中使用它,但是我看不到瀏覽器中的樹枝模板,我唯一想到的是$ data響應。

在配置中我應該在哪里指定模板? 我到底應該在哪里放置模板?

AppBundle / Controller / 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;
    }
}

配置文件

#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

如果您使用DefaultController來提供html,則可以嘗試以下操作

class DefaultController
{
  /**
   * @Route("/", name="homepage")
   *
   */
  public function indexAction(Request $request)
  {
    $twigData = array('data' => 'string');

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


  }
}

然后,您可以使用{{ data }}在index.html.twig中引用data變量。

另外一個建議是,您始終可以在api路由上使用nginx或apache重定向到相關存儲庫根文件夾,而不是對cors請求使用捆綁包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM