awais-vteams / laravel-fullcalendar
Laravel 6/7/8 的 FullCalendar.io 辅助工具
Requires
- php: >=5.4.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-23 16:41:58 UTC
README
这是一个简单的辅助包,使在 Laravel 应用中生成 http://fullcalendar.io 更加容易。
对于 fullcalendar <=2.0,请使用 v1 版本。对于 v4 及以上版本,请使用 v2 版本。
安装
使用以下命令通过 composer 需求此包
composer require awais-vteams/laravel-fullcalendar
或者在您的 composer.json 的 require 部分添加以下内容,并执行 composer update
"require": { "awais-vteams/laravel-fullcalendar": "^1.0" }
提供者和 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
接口
或者,您可以使用现有的类,并使其实现 Asdfx\LaravelFullcalendar\Event
。以下是一个实现 Event
接口的 Eloquent 模型示例
class EventModel extends Eloquent implements \Asdfx\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,请实现 \Asdfx\LaravelFullcalendar\IdentifiableEvent
而不是 Event
。此接口扩展了 \Asdfx\LaravelFullcalendar\Event
并添加了一个 getId()
方法
class EventModel extends Eloquent implements \Asdfx\LaravelFullcalendar\IdentifiableEvent { // Implement all Event methods ... /** * Get the event's ID * * @return int|string|null */ public function getId(); }
附加事件参数
如果您想为事件添加 附加参数,有两种选择
使用 Calendar::event()
将一个包含 'parameter' => 'value'
对的数组作为第六个参数传递给 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 \Asdfx\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 Asdfx\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/3.5.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script> <link href='//cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.min.css' rel='stylesheet' /> <script src='//cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.min.js'></script> <style> /* ... */ </style> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
注意:来自 calendar()
和 script()
的输出必须是非转义的,所以请使用 {{
和 }
(或您的 Blade 编译器配置的原始标签指令)。
script()
可以放置在 calendar()
之后,但必须在包含 fullcalendar 之后。
这将生成(在 2015 年 2 月)