无法使FullCalendar工作(Laravel 5)

时间:2015-06-05 08:51:28

标签: php laravel model-view-controller fullcalendar laravel-5

我在Laravel 5上很新,我一直在尝试按照Github上的说明安装完整的日历(在https://github.com/maddhatter/laravel-fullcalendar上说明)

目前我只是想在我的网站上显示日历,所以我做了以下事情:

通过作曲家安装插件:'composer require maddhatter / laravel-fullcalendar'&为我的app.php添加了一个别名。

接下来我创建了一个扩展Eloquent \ Model的接口:(当我向我的数据库添加事件时):

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent{

    //fillable = Being able to mass assign (MassAssignment exception)

    protected $dates = ['start', 'end'];
    /**
     * Get the event's id number
     *
     * @return int
     */
    public function getId();

    /**
     * Get the event's title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Is it an all day event?
     *
     * @return bool
     */
    public function isAllDay()
    {
        return (bool)$this->all_day;
    }

    /**
     * Get the start time
     *
     * @return DateTime
     */
    public function getStart()
    {
        return $this->start;
    }

    /**
     * Get the end time
     *
     * @return DateTime
     */
    public function getEnd()
    {
        return $this->end;
    }

}

然后在我的控制器中我添加了:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class CalendarController extends Controller {

$events[] = \Calendar::event(
    'Event One', //event title
    false, //full day event?
    '2015-06-05T0800', //start time (you can also use Carbon instead of DateTime)
    '2015-06-05T0800', //end time (you can also use Carbon instead of DateTime)
    0 //optionally, you can specify an event ID
);

$eloquentEvent = EventModel::first(); //EventModel implements MaddHatter\LaravelFullcalendar\Event  

$calendar = \Calendar::addEvents($events) //add an array with addEvents
    ->addEvent($eloquentEvent, [ //set custom color fo this event
        'color' => '#800',
    ])->setOptions([ //set fullcalendar options
        'firstDay' => 1
    ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
        'viewRender' => 'function() {alert("Callbacks!");}'
]); 

return view('pages.calendar', compact('calendar'));

}

执行此操作后,在说明中说我必须将以下内容添加到我的视图中(位于我的文件夹中:resources / pages / calendar.blade.php):

@extends('masterpage')
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css"/>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.print.css"/>


@section('content')

<h1> Calendar </h1>
{!! $calendar->calendar() !!}
{!! $calendar->script() !!}
@stop

我也在我的项目中添加了'$ bower install fullcalendar'来添加它们。

此外,这是我的文件夹结构,看到我可能错过了一些东西: folder structure

执行完所有这些步骤后,它会显示日历应显示但我收到此错误: Undefined variable: calendar

可能我只是愚蠢,但我该如何解决这个问题呢?

2 个答案:

答案 0 :(得分:3)

你最终让这个工作了吗?

我在实施自己时遇到了一些问题,并认为我会在解决这个问题上添加经验。

这对我使用了fedora上的laravel 5.1和mysql(mariadb)。

  1. 模型类似于以下内容(从https://github.com/maddhatter/laravel-fullcalendar复制而来),并对
  2. 中的以下行进行了更改
      

    类EventModel扩展了Eloquent实现   \ MaddHatter \ LaravelFullcalendar \事件

      

    类EventModel扩展了Model实现   \ MaddHatter \ LaravelFullcalendar \事件

    1. 将以下行添加到控制器
    2.   

      使用App \ EventModel;使用MaddHatter \ LaravelFullcalendar \ Event;

      1. 对于db事件,将其添加到控制器
      2. $event = EventModel::all();
        
        foreach ($event as $eve) {
          $events[] = \Calendar::event(
          $eve->title, //event title
          $eve->allDay, //full day event?
          $eve->start, //start time (you can also use Carbon instead of DateTime)
          $eve->end, //end time (you can also use Carbon instead of DateTime)
          $eve->id //optionally, you can specify an event ID
          );
        }
        
        1. 如果您使用刀片模板,我必须将脚本添加到我的主页和日历页面(如果从一个页面中删除它不会显示日历)

        2. 脏易路由

        3.   

          路线::资源( '日历', 'CalendarController');

          我是laravel的新手所以我知道可能有更简单的方法,或者我没有遵循惯例,但这是我到目前为止工作的方式,希望它有所帮助。

答案 1 :(得分:1)

看起来控制器中的代码不在方法中:

class CalendarController extends Controller {

    public function index() { //code should be inside a method
        $events[] = \Calendar::event(
            'Event One', //event title
            false, //full day event?
            '2015-06-05T0800', //start time (you can also use Carbon instead of DateTime)
            '2015-06-05T0800', //end time (you can also use Carbon instead of DateTime)
            0 //optionally, you can specify an event ID
        );

        $eloquentEvent = EventModel::first(); //EventModel implements MaddHatter\LaravelFullcalendar\Event  

        $calendar = \Calendar::addEvents($events) //add an array with addEvents
            ->setOptions([ //set fullcalendar options
                'firstDay' => 1
            ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
                'viewRender' => 'function() {alert("Callbacks!");}'
        ]); 

        return view('pages.calendar', compact('calendar'));
    }

}

更新

您的EventModel getId()方法也需要有正文(即:它需要有一个开始/结束{},以及做某事):

/**
 * Get the event's id number
 *
 * @return int
 */
public function getId() {
    return $this->id;
}

该项目的readme.md缺少示例正文。

更新2:

class EventModel extends Model \MaddHatter\LaravelFullcalendar\IdentifiableEvent

应该是

class EventModel extends Model implements \MaddHatter\LaravelFullcalendar\IdentifiableEvent