CakePHP中的事件和完整日历

时间:2014-07-25 10:35:11

标签: cakephp fullcalendar

我在CakePHP中设置了FullCalendar插件,我可以看到它,它显示了,我正在尝试将我的PHP控制器(视图管理员提要)中的事件放到我的完整日历插件中。我不知道什么是错的,我的日历中没有显示任何事件。如果有人能找到我的calandar为空的原因,我将感激不尽。 看看屏幕,这就是我在控制台中的内容:

enter image description here

答案是NULL。

JS文件:

// JavaScript Document

    $(document).ready(function() {

        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
                defaultView: 'agendaWeek',
                firstHour: 8,
                weekMode: 'variable',
                aspectRatio: 2,
                editable: true,
            events: {
                url: FcRoot + '/events/feed',
                color: 'yellow',    // an option!
                textColor: 'black',  // an option!
            },
            eventRender: function(event, element) {
            ///
            }
        });

    });

EventsController

// The feed action is called from JS to get the list of events (JSON)
    public function admin_feed($id=null) {
        $this->layout = "ajax";
        $vars = $this->params['url'];
        $conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
        $events = $this->Event->find('all', $conditions);
        foreach($events as $event) {
            if($event['Event']['all_day'] == 1) {
                $allday = true;
                $end = $event['Event']['start'];
            } else {
                $allday = false;
                $end = $event['Event']['end'];
            }
            $data[] = array(
                    'id' => $event['Event']['id'],
                    'title'=>$event['Event']['title'],
                    'start'=>$event['Event']['start'],
                    'end' => $end
            );
        }
        $this->set("json", json_encode($data));
    }

在View / Events / admin_feed

<?php 
echo $json; 
?>

现在回复:

enter image description here

2 个答案:

答案 0 :(得分:1)

不要设置它,只需回显它

echo json_encode($data);

设定值应该在视图中使用,在您的情况下,您只需返回&#39;作为ajax回应

对于ajax调用,根本没有必要进行查看,将$this->autoRender = false;添加到您的函数中:这将阻止&#39;寻找&#39;用于查看文件。

答案 1 :(得分:1)

您应该真正利用路由和视图。它允许您正确地组织代码并保持干燥。

 public function admin_feed($id=null) {
    $vars = $this->params['url'];
    $conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
    $events = $this->Event->find('all', $conditions);
    foreach($events as $event) {
        if($event['Event']['all_day'] == 1) {
            $allday = true;
            $end = $event['Event']['start'];
        } else {
            $allday = false;
            $end = $event['Event']['end'];
        }
        $data[] = array(
                'id' => $event['Event']['id'],
                'title'=>$event['Event']['title'],
                'start'=>$event['Event']['start'],
                'end' => $end
        );
    }
    $this->set("events", $data);
}

现在,您的admin_feed操作不仅可以用作json提要(即使这可能只是您想要的)。这也使测试更容易。

将以下内容添加到您的路线中,告诉Cake您要允许json扩展名:

Router::parseExtensions('json');

然后,将RequestHandler添加到控制器组件中。当找到json扩展名时,此组件将自动切换到您的json视图和布局。

接下来,为/View/Layout/json/default.ctp中的所有json视图添加布局:

<?php
echo $this->fetch('content');

然后,在/View/Events/json/admin_feed.ctp中添加您的观点:

<?php
echo json_encode($events);

就是这样。现在,如果您想使用admin_feed来查看HTML中的事件,可以为其添加一个视图。

您的完整日历网址现在应为:FcRoot + '/admin/events/feed.json'。尝试在浏览器中访问它,看看你是否看到了json。