dsj2014 2017-03-27 15:54
浏览 82

Symfony变量不存在错误消息

I am new to Symfony framework. I am having some difficulties in pulling data from database. Here is my controller class:

class CrisisCtrl extends Controller{
public function listAction(Request $request)
{
    $locations = $this->getDoctrine()->getRepository('AppBundle:Crisis')->findAll();
    return $this->render(':index.html.twig', array(
        'locations' => $locations
    ));
}
}

I followed an online tutorial to type all these out manually. When I typed $this->, I tried to find getDoctrine() from the suggestion list, however, I got nothing to select, therefore I typed them out manually and I not sure if this will cause any problem or not because apparently my code have some errors for now.

Inside my index.html.twig, I got these:

{% extends 'base.html.twig' %}

{% block page_content %}
// all the HTML
{% endblock %}
{% block custom_javascripts %}
<!-- Custom Theme Scripts -->
<script src="...//removed code"
        async defer></script>

<script>
{% for location in locations %}
    var map;
    function initMap() {
        var myLatLng = {lat: '{{ location.latitude }}', lng: '{{ location.latitude }}' };
        map = new google.maps.Map(document.getElementById('map'), {
            center: myLatLng,
            zoom: 10
        });

        var marker = new google.maps.Marker({
           position: myLatLng,
           map: map,
           title: 'Hello World!'
       });
   }
{% endfor %}
</script>

{% endblock %}

However, I am getting error message saying the variable 'locations' does not exist and the error is highlighted at the for loop in twig. I have been researching on other ways to fetch data from database and all of them are doing the same way.

I have no idea why is it so. Any ideas? Thanks in advance.

UPDATED

In my DefaultController class, I have these:

class DefaultController extends Controller
{
    /**
     * @Template("AppBundle:Default:index.html.twig")
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        return array();
    }
}

The problem now is that the action is not calling the CrisisCtrl class. It is now executing the function above. I am not allowed to touch this function because if I changed it, other functions will be malfunctioned.

So in my CrisisCtrl class, before the listAction, I added these:

 /**
 * @Template("AppBundle:Default:index.html.twig")
 * @Route("/", name="homepage")
 */

My file structure as such:

enter image description here

and my JavaScript is nested inside the index.html.twig. So I wonder how do I actually specify the route?

  • 写回答

2条回答 默认 最新

  • doujieluo5875 2017-03-27 15:58
    关注

    from symfony controller you should use

    $this->getDoctrine()->getManager()->getRepository()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝