简体   繁体   中英

Symfony 2.8 - Access (local) JSON from view?

I have a symfony2 app with a couple of bundles and assetic which handles my css and js.

I would like to also deploy a dataset which the view will need to draw a map - let's call it map.json .

Where should I put this file so I can access it with relative ease from a view (twig template) or other javascript file?


EDIT: In one of the controllers (written a couple years ago...) I retrieved a JSON document as follows:

$myFile = $this->get('kernel')->getRootDir() . '/../web/common/path/to/file.json';
$parameters['myFile'] = file_get_contents($myFile);

And then returned $parameters with the view. It works, but seems a bit strange... and it doesn't help me if I need to access the file from an external javascript file (eg. myJs.js ).

I also don't particularly like having to hardcode asset paths, especially if I also have to do it to access it via external javascript.

您的位置json-file必须由拥有www-data的用户,而不是由被阻止.htaccess让你无论是把它放到web文件夹,或者把它放到你的一个bundles Resources/public文件夹被simlinked您webfolder 。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM