【问题标题】:Displaying JSON Data in my Twig template in symfony2在 symfony2 的 Twig 模板中显示 JSON 数据
【发布时间】:2012-04-15 04:46:10
【问题描述】:

我正在尝试在我的应用程序中使用 jquery 扩展作为 Event calendar。我先用core php试了一下,效果很好。现在我想在我的 symfony2 应用程序中使用它,但我无法在我的 twig 文件中显示它。这是我用来渲染的代码,但我不确定我在哪里做错了。

控制器

public function studentCalendarAction()
{
  $year = date('Y');
  $month = date('m');
  $response = new Response(json_encode(array(
        array(
                'id' => 111,
                'title' => "Event1",
                'start' => "$year-$month-10",
                'url' => "http://yahoo.com/"
        ),
  )));
  $response->headers->set('Content-Type', 'application/json');
  return $this->render('CollegeStudentBundle:StudentAttendance:calendar.html.twig',array('response'=>$response));
}

路由器

CollegeStudentBundle_attendance_calendar:
    pattern:  /student/attendance/calendar
    defaults: { _controller: CollegeStudentBundle:StudentAttendance:studentCalendar }
    requirements:
        _method:  GET|POST

树枝

    <script type='text/javascript'>

                $(document).ready(function() {

                $('#calendar').fullCalendar({

                    editable: true,

                    events: "../../student/attendance/calendar",

                    eventDrop: function(event, delta) {
                        alert(event.title + ' was moved ' + delta + ' days\n' +
                            '(should probably update your database)');
                    },

                    loading: function(bool) {
                        if (bool) $('#loading').show();
                        else $('#loading').hide();
                    }

                });

            });

    </script>


</head>

<body>
    <div id='calendar'></div>
</body>

即使我试图让我的Router 像这样

CollegeStudentBundle_attendance_calendar:
    pattern:  /student/attendance/calendar
    defaults: { _controller: CollegeStudentBundle:StudentAttendance:studentCalendar, _format: json }
    requirements: { _format: (xml|json), _method: GET }  

但这会显示树枝文件的代码。

【问题讨论】:

    标签: json symfony


    【解决方案1】:

    只返回创建的$response,而不是呈现的响应。

    编辑:您修改后的studentCalendarAction 将是,

    public function studentCalendarAction()
    {
      $year = date('Y');
      $month = date('m');
    
      $jsonData = json_encode(array(
            array(
                    'id' => 111,
                    'title' => "Event1",
                    'start' => "$year-$month-10",
                    'url' => "http://yahoo.com/"
            ),
      ));
    
      $headers = array(
            'Content-Type' => 'application/json'
      );
    
      $response = new Response($jsonData, 200, $headers);
      return $response;
    }
    

    另外我认为树枝文件是从不同的路线呈现的,而不是CollegeStudentBundle_attendance_calendar 路线。

    【讨论】:

    • 感谢您的回复。你能详细说明你的答案吗?我到底需要在哪里进行更改。控制器或视图。
    • 在控制器的studentCalenderAction方法中。解析 Twig 文件后,渲染返回只是另一个新的 Response 对象。不是您使用 json 编码数组创建的 Response 对象。
    • return new JsonResponse(array('id' => 111,...));可能会有所帮助,不要忘记使用 Symfony\Component\HttpFoundation\JsonResponse;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2021-12-11
    相关资源
    最近更新 更多