Laravel wrapper for Google Calendar API that utilizes the Google Client API. Works seamlessly with FullCalendar.io.
[TOC]
This package can be used in Laravel 5.4 or higher.
You can install the package via composer:
composer require hackeresq/laravel-google-calendar
In Laravel 5.5+ the service provider will automatically get registered and you can skip this step. In older versions of the framework just add the service provider in config/app.php
file:
'providers' => [
// ...
hackerESQ\GoogleCalendar\GoogleCalendarServiceProvider::class,
];
The same is true for the alias. If you're running Laravel 5.5+, you can also skip this step. In older versions of the framework just add the alias in config/app.php
file:
'aliases' => [
// ...
'GoogleCalendar' => hackerESQ\GoogleCalendar\Facades\GoogleCalendar::class,
];
You can publish the migration and config files and update the user table with:
php artisan vendor:publish --provider="hackerESQ\GoogleCalendar\GoogleCalendarServiceProvider" && php artisan migrate
Success! laravel-google-calendar is now installed!
The first step to properly configure laravel-google-calendar, you need Google API credentials. You can obtain these credentials at https://console.developers.google.com/apis/credentials/oauthclient/.
Copy and paste the Google API credentials into your .env
file. For example:
GOOGLE_CLIENT_ID=C13n71D_7166xlnk4q4fd24hdeteq.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=s3cr37sMnYiOk4i8fr2rX
GoogleCalendar can be accessed using the easy-to-remember Facade, "GoogleCalendar."
Gets the interstitial Google Client API authentication URL.
Optional
$redirect
: Pass a relative URL (e.g. '/dashboard') to redirect user after authenticated with Google. Defaults to site root (i.e. '/').
Checks if currently logged in user is authenticated with Google Client API.
Lists the available calendars for the currently logged in user.
Optional
$accessToken
: Helpful when you need to manually set a user's access token (e.g. using API authentication rather than Web). Defaults to 'google_access_token' column in theusers
table.
Lists the events for the currently logged in user.
Optional
$calendarId
: Manually set Calendar ID. Defaults to 'primary' calendar.Optional
$accessToken
: Helpful when you need to manually set a user's access token (e.g. using API authentication rather than Web). Defaults to 'google_access_token' column in theusers
table.Optional
$start
: Defaults to beginning of current month.Optional
$end
: Defaults to end of current month.Optional
$timezone
: Defaults to null.
Required
$request
:Optional
$accessToken
: Helpful when you need to manually set a user's access token (e.g. using API authentication rather than Web). Defaults to 'google_access_token' column in theusers
table.
Required
$request
:Optional
$accessToken
: Helpful when you need to manually set a user's access token (e.g. using API authentication rather than Web). Defaults to 'google_access_token' column in theusers
table.
Required
$request
:Optional
$accessToken
: Helpful when you need to manually set a user's access token (e.g. using API authentication rather than Web). Defaults to 'google_access_token' column in theusers
table.
This package comes with a Controller that can be used (or you can create your own) in order to expose the above methods to an API. Here's a sample Routes definition to do this:
// Google Calendar API routes
Route::group([ 'prefix'=>'api/google' ], function () {
Route::get('calendars', '\hackerESQ\GoogleCalendar\Controllers\GoogleCalendarController@listCalendars');
Route::get('events', '\hackerESQ\GoogleCalendar\Controllers\GoogleCalendarController@listEvents');
Route::put('events', '\hackerESQ\GoogleCalendar\Controllers\GoogleCalendarController@updateEvent');
Route::post('events', '\hackerESQ\GoogleCalendar\Controllers\GoogleCalendarController@createEvent');
Route::delete('events/{id}', '\hackerESQ\GoogleCalendar\Controllers\GoogleCalendarController@deleteEvent');
});
These routes definitions will expose API functionality at 'api/google/calendars' and 'api/google/events' that you can consume via AJAX calls on your site.
You can configure this package to input and output data in a format that can be understood by FullCalendar.io. To do so, you can modify the 'format' property in the google_calendar.php
config file as such:
return [
//....
'format' => 'fullcalendar',
//....
];
Then, when you initialize FullCalendar.io, you can consume the API exposed by following the steps in the Expose API above as such:
$('#calendar').fullCalendar({
eventSources: [
// your event source
{
url: 'api/google/events',
type: 'GET',
data: {
calendar: 'primary', // define your google calendar id here
},
error: function() {
alert('there was an error while fetching events!');
}
}
// any other sources...
]
});
Feel free to create a fork and submit a pull request if you would like to contribute.
Raise an issue on GitHub if you notice something broken.