I am trying to setup API to use it in SPA but I can't see the twig template in browser the only think I get is $data response.
Where in config I should specify the template? Where exactly should I put templates?
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;
}
}
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
If you're using the DefaultController
for serving html you can try the following
class DefaultController
{
/**
* @Route("/", name="homepage")
*
*/
public function indexAction(Request $request)
{
$twigData = array('data' => 'string');
return $this->render('BundleName:FolderName:index.html.twig', $twigData);
}
}
You can then reference the data variable in the index.html.twig by using {{ data }}
Also another suggestion, rather than using a bundle for cors request you can always have an nginx or apache redirect on your api routes to the concerned repository root folder