繁体   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