progcode / laravel-fullcalendar
Laravel 对 FullCalendar.io 的辅助工具
Requires
- php: >=5.4.0
- illuminate/support: ~5.0|^6.0|^7.0
README
这是一个简单的辅助包,旨在使在 Laravel 应用程序中生成 http://fullcalendar.io 更加容易。
安装
使用以下命令通过 composer 需求此包
composer require progcode/laravel-fullcalendar
或者将以下内容添加到你的 composer.json 的 require 部分,然后运行 composer update
"require": { "progcode/laravel-fullcalendar": "~1.3.3" }
Laravel 5.5+
提供者和 Calendar
别名将自动注册。
你还需要在你的 HTML 中包含 fullcalendar.io 的文件。
使用方法
创建事件
使用 event()
创建事件最简单的方式是将事件信息传递给 Calendar::event()
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io' ] );
实现 Event
接口
或者,你可以使用一个现有的类,使其实现 MaddHatter\LaravelFullcalendar\Event
。以下是一个实现 Event
接口的 Eloquent 模型示例
class EventModel extends Eloquent implements \MaddHatter\LaravelFullcalendar\Event { protected $dates = ['start', 'end']; /** * Get the event's id number * * @return int */ public function getId() { return $this->id; } /** * 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; } }
IdentifiableEvent
接口
如果你希望你的现有类有事件 ID,则可以实现 \MaddHatter\LaravelFullcalendar\IdentifiableEvent
而不是。此接口扩展了 \MaddHatter\LaravelFullcalendar\Event
并添加了 getId()
方法
class EventModel extends Eloquent implements \MaddHatter\LaravelFullcalendar\IdentifiableEvent { // Implement all Event methods ... /** * Get the event's ID * * @return int|string|null */ public function getId(); }
附加事件参数
如果你想要向你的事件添加额外的参数,有两种选择
使用 Calendar::event()
将一个 'parameter' => 'value'
对的数组作为第 6 个参数传递给 Calendar::event()
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io', //any other full-calendar supported parameters ] );
在你的事件类中添加 getEventOptions
方法
<?php class CalendarEvent extends \Illuminate\Database\Eloquent\Model implements \MaddHatter\LaravelFullcalendar\Event { //... /** * Optional FullCalendar.io settings for this event * * @return array */ public function getEventOptions() { return [ 'color' => $this->background_color, //etc ]; } //... }
创建日历
要创建日历,在你的路由或控制器中创建你的事件,然后将它们传递给 Calendar::addEvent()
或 Calendar::addEvents()
(用于添加事件数组)。addEvent()
和 addEvents()
可以流畅地使用(链式使用)。它们的第二个参数接受一个有效的 FullCalendar 事件对象参数 数组。
示例控制器代码
$events = []; $events[] = \Calendar::event( 'Event One', //event title false, //full day event? '2015-02-11T0800', //start time (you can also use Carbon instead of DateTime) '2015-02-12T0800', //end time (you can also use Carbon instead of DateTime) 0 //optionally, you can specify an event ID ); $events[] = \Calendar::event( "Valentine's Day", //event title true, //full day event? new \DateTime('2015-02-14'), //start time (you can also use Carbon instead of DateTime) new \DateTime('2015-02-14'), //end time (you can also use Carbon instead of DateTime) 'stringEventId' //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('hello', compact('calendar'));
示例视图
然后,要将以下代码添加到你的视图中以显示
<!doctype html> <html lang="en"> <head> <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"/> <style> /* ... */ </style> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
注意:从 calendar()
和 script()
产生的输出必须是非转义的,因此请使用 !!
和 !!
(或任何你配置的 Blade 编译器原始标签指令)。
可以将 script()
放在 calendar()
之后的位置,并且必须在包含 fullcalendar 之后。
这将生成(2015 年 2 月)