简体   繁体   English

Symfony 2.8-从视图访问(本地)JSON?

[英]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. 我有一个symfony2应用程序,其中包含几个捆绑软件和处理我的CSS和js的资产。

I would like to also deploy a dataset which the view will need to draw a map - let's call it map.json . 我还想部署一个数据集,视图将需要该数据集来绘制地图-我们将其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? 我应该将该文件放在哪里,以便可以从视图(树枝模板)或其他javascript文件相对轻松地访问它?


EDIT: In one of the controllers (written a couple years ago...) I retrieved a JSON document as follows: 编辑:在其中一个控制器(几年前写的...)中,我检索了一个JSON文档,如下所示:

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

And then returned $parameters with the view. 然后返回带有视图的$parameters 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 ). 它可以工作,但似乎有点奇怪……如果我需要从外部javascript文件(例如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. 我也不太喜欢必须对资产路径进行硬编码,尤其是如果我还必须通过外部JavaScript访问它的话。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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