I needed to integrate a calendar into my application.
This being a CakePHP application, I started looking for a good calendar helper – but found none that I liked enough to integrate. Luckily, I then stumbled upon the excellent fullCalendar project which is everything I wanted:
- Extensible – let me change events easily.
- Provides lazy loading of events.
- Provides for visual differentiation of events based on their type.
- Uses multiple displays (daily, weekly, monthly).
- Lets me, as the developer, store events that are not editable by the user (e.g. Easter dates).
Integration with Cake was relatively straight forward, and I found this series that helped me get started.
FullCalendar is a jQuery plugin. To use it, you’ll need jQuery library itself and a few jQuery plugins. You don’t need to go and find them all over the Internet – they are all bundled with the fullCalendar download.
To set up your cake with fullCalendar you’ll first need to download and unzip fullCalendar’s download package to cake’s /app/webroot/js folder.
Then you’ll want to hook up fullCalendar’s css and javascript files to your page. It’s pretty straightforward actually. You may use the Javascript helper or add the script tags to the view file yourself.
To use the Javascript helper, add it to the $helpers array in your controller (my calendar controller is just called events_controller):
var $name = 'Events';
var $helpers = array('Admin','Time','Javascript');
}
Then link the relevant javascript files in your view. I just add them to the relevant *.ctp file (calendar.ctp in my case) – but you may add them to the layout itself if you want.
<?php
echo $javascript->link('jquery-1.3.2.min.js');
echo $javascript->link('ui.core.js');
echo $javascript->link('ui.resizable.js');
echo $javascript->link('fullcalendar.min.js');
echo $javascript->link('ui.draggable.js');
echo $html->css('fullcalendar');
//Note: to use $html->css as above, the fullcalendar.css
//file must be in your app/webroot/css folder.
?>
<div id="calendar"></div>
The “calendar” div above is the where the fullCalendar will actually be created once you call the fullCalendar function in your document.ready function
3. To display the calendar, you need to call its init method – fullCalendar.
$(document).ready(function() {
$('#calendar').fullCalendar({});
});
</script>
The code above will display the calendar with its default configuration. It will have no events, because none are defined.
If you did everything right you should see something like the screenshot below. This means that you managed to hook up fullCalendar and CakePHP.
And that’s it for integration.
The next post in this series describes how to hook up a database table as an events source for this integration.
The fullCalendar and CakePHP Series:
- Part 1: Set up
- Part 2: Creating an event source
- Part 3: Adding events
- Part 4: Editing events
- Part 5: Dragging and resizing
Popularity: 87% [?]

Thanks a lot it really helped me, I was following the guide you made reference but it wasn´t so clear as yours.. thanks a lot
By te way… love the hot girls that you show in each post..
… we geeks need some distraction lol
@Andres – thanks! I will be posting the rest of the series in the coming weeks, hope it will help.
As for the girls – yeah… I try to help
Hey i am not able to integrate it.. got stuck in the 3rd step.. how the init functions is called?
@Thakurdas – the code in the third step needs to be in the calendar view file. Make sure that you have all the javascript files added there as well (from the previous step). What doesn’t work?
Ya i have added it to calendar.ctp.. checked every Javascript link.. but not able to get the calendar screenshot.. Thanks for replying… may be i am doing some other mistakes..
@Thakurdas – can you post your view file? also – can you check with Firebug that there are no javascript errors when the page loads?
ya i will certainly should i post here.. ? or let me know ur id.. firebug detected 5 errors one in view and other in js files… new to all this dont know where to fix it.. basically the ‘)’ the tag…
@Thakurdas – post here, also the errors in firebug. I’ll see what I can do.
// calendar.ctp
$(document).ready(function() {
$(‘#calendar’).fullCalendar({});
}
link(‘/js/jquery/ui.core.js’);
echo $javascript->link(‘/js/jquery/ui.resizable.js’);
echo $javascript->link(‘/js/fullcalendar.min.js’);
echo $javascript->link(‘/js/jquery/ui.draggable.js’);
echo $html->css(‘fullcalendar’);
//Note: to use $html->css as above, the fullcalendar.css
//file must be in your app/webroot/css folder.
?>
firebug errors:
1. missing ) after argument list
[Break on this error] \n
calendar (line 23)
2. jQuery is not defined
[Break on this error] ;jQuery.ui || (function($) {
ui.core.js (line 10)
3. jQuery is not defined
[Break on this error] })(jQuery);
ui.resizable.js (line 800)
4. jQuery is not defined
[Break on this error] (function(D){var I=D.fullCalendar={};v…th|height)\s*:[^;]*/ig,”")}})(jQuery);
fullca….min.js (line 17)
5. jQuery is not defined
[Break on this error] })(jQuery);
ui.draggable.js (line 766)
@Thakurdas – you need to link to the jquery library itself – it is the first link in calendar.ctp above. Without it – all the other plugins can not work.
$(document).ready(function() {
$(‘#calendar’).fullCalendar({});
}
link(‘/js/jquery.js’);
echo $javascript->link(‘/js/jquery/ui.core.js’);
echo $javascript->link(‘/js/jquery/ui.resizable.js’);
echo $javascript->link(‘/js/fullcalendar.min.js’);
echo $javascript->link(‘/js/jquery/ui.draggable.js’);
echo $html->css(‘fullcalendar’);
//Note: to use $html->css as above, the fullcalendar.css
//file must be in your app/webroot/css folder.
?>
i did the changes but still this error is coming and i am not getting the calendar screenshot other errors are ommited
missing ) after argument list
http://localhost/fullcalendar/events/calendar
Line 22
Thanks Anyway
@Thakurdas – Thanks, that was ommitted from the code
– fixed it now – happy you found it.
$ is not defined
http://localhost/fullcalendar/events/calendar
Line 18..
got this error afterwards copying the updated code..
@Thakurdas – what does the resulting html look like when it’s complete?
nothing just a blank page..
@Thakurdas – I mean the source – does the source contain nothing at all? A blank page means that there’s a js error somewhere there. This is usually a case in Internet Explorer when the last configuration value ends with a comma or something similar.. Can you view->source and paste it here?
Cheers.
CakePHP: the rapid development php framework: Events
CakePHP: the rapid development php framework
$(document).ready(function() {
$(‘#calendar’).fullCalendar({});
});
@Thakurdas – is this the entire source of the resulting HTML? If so – then
a) The javascript libs aren’t anywhere,
b) You don’t have a div with id=”calendar”…
actually wen i am pasting
only this much code is displayed.. therefore i pasted twice.. pasting it again lets see…
CakePHP: the rapid development php framework: Events
CakePHP: the rapid development php framework
$(document).ready(function() {
$(‘#calendar’).fullCalendar({});
});
nah the entire thing is not being pasted here… any other way i can send you..?
Thanks
great article just what the doctor ordered……………………….thankz for such a sweet explanation and easy to use terminology.
Hi,
I am implementing full calendar in one of my cake-php project but after implementing it dosen’t load the calendar but asks me download the file. If I download the file and open it, there is json response in that file. Need your help.
I am using it like this:
1. I have a function calendarfeed in one of my controller, fetch the data from databsae and then convert it into json format and then echo the json response.
2. in calendarfeed.ctp file I have added the css, required javascript and then called the function like you explained.
but when I run the url it ask me to download the calendar instead of displaying it.
@Arry – I am not exactly sure what you’re trying to do.
Can you paste the code for your view here?
Thanks.
Hi,
Thanks for such a quick response.
link(‘jquery/jquery.js’);
echo $javascript->link(‘jquery/jquery-ui-custom.js’);
echo $javascript->link(‘fullcalendar.min.js’);
echo $html->css(‘fullcalendar’);
//Note: to use $html->css as above, the fullcalendar.css
//file must be in your app/webroot/css folder.
?>
$(document).ready(function() {
$(‘#calendar’).fullCalendar({
//events: “/portfolios/calendarfeed”
events: “http://local.release-6-5.com/portfolios/calendarfeed”
});
});
@Arry-
I assume the first line is the full echo $javascript->… ?
What file does it ask you to download?
Also – did you add a div element with an id=”calendar” ?
Hi,
Here is the code that I am using:
when I add the “$this->header(‘Content-Type: application/json’);” it asks me download the file. and that file contains the json string created by the below function.
1. Controller File Code:
var $helpers = array(‘Html’, ‘Form’, ‘Time’, ‘Ajax’, ‘Javascript’,'Paginator’);
var $components = array(‘Auth’, ‘RequestHandler’);
function calendarfeed()
{
//1. Transform request parameters to MySQL datetime format.
if(!isset($this->params['url']['start']))
{
$mysqlstart = date( ‘Y-m-01′);
$mysqlend = date(‘Y-m-t’);
}
else
{
$mysqlstart = date( ‘Y-m-d H:i:s’, $this->params['url']['start']);
$mysqlend = date(‘Y-m-d H:i:s’, $this->params['url']['end']);
}
$arrHPDViolations = $this->getcountofeach1($mysqlstart, $mysqlend);
//debug($arrHPDViolations);
//3. Create the json array
$rows = array();
for ($a=0; count($arrHPDViolations)> $a; $a++) {
//Is it an all day event?
$all = true;
//Create an event entry
$rows[] = array(‘id’ => $a,
‘title’ => “HPD Vio “.$arrHPDViolations[$a][0]['HpdVioCount'],
‘start’ => $arrHPDViolations[$a]['Hpdviolation']['nov_issued_date']. ” 05:00″,
‘end’ => $arrHPDViolations[$a]['Hpdviolation']['nov_issued_date']. ” 06:00″,
‘allDay’ => $all,
);
}
$this->set(‘events’, json_encode($rows));
//debug($rows);
//exit;
//4. Return as a json array
Configure::write(‘debug’, 0);
$this->autoRender = false;
$this->autoLayout = false;
$this->header(‘Content-Type: application/json’);
echo json_encode($rows);
}
It Creates the json string.
2. calendarfeed.ctp File code it located in views/portfolio folder.
link(‘jquery/jquery.js’);
echo $javascript->link(‘jquery/jquery-ui-custom.js’);
echo $javascript->link(‘fullcalendar.min.js’);
echo $html->css(‘fullcalendar’);
//Note: to use $html->css as above, the fullcalendar.css
//file must be in your app/webroot/css folder.
?>
$(document).ready(function() {
$(‘#calendar’).fullCalendar({
// US Holidays
events: ‘http://local.release-6-5.com/portfolios/calendarfeed’
});
});
The div is also included in the ctp file
@Arry – I think I see what you do here.
This post (part I) talks about the calendar.ctp view – where you show the calendar to your user.
The second post (part II) – which is where you took the json example from, talks about a different view – only a feed.
What you’re doing in your code – is that you use calendarfeed.ctp to both display the calendar and as the json events source – this is not going to work – you have to separate it into two: calendar.ctp (or whichever name you want) – to display the calendar, and have the calendar div etc – and then a separate feed.ctp or calendarfeed.ctp – or whatever, which is where you create the json array.
Cheers.
Not sure if get it write.
I followed exactly the same instructions given in “Part 2: Creating an event source”
What I need to do is to show the events in a calendar using the event source.
Please correct me if I an wrong.
1. In controller create a function which will create the json feed
2. in views/portfolios folder create a ctp file which will include the javascripts required, div id calendar, and the calendar initialization
$(document).ready(function() {
$(‘#calendar’).fullCalendar({
events: “/events/feed”,
});
}
.
The proble which I am facing is when I hardcode the events with the json string it works and if I replace that json srting with the event feed url as above it dosent work.
@Arry – yes – this is exactly what would happen.
The ctp file in your previous comment (inside /views/portfolio) is ok. And you can see that it’s ok because you get events if you hardcode them.
The problem is with the feed:
You write this :
events: “/events/feedâ€,
This means that the calendar expects the events to come from a function called feed inside events_controller.
but, in your code a few comments before – you put a function called:
function calendarfeed()
which (I think) you put in your portfolio controller.
I think the function itself is ok (I am not sure why you have the line
$this->set(‘events’, json_encode($rows));
You only need the echo… at the end)
But – the name of the function is different to the one you are using in your view:
You probably want to put:
events: “/events/calendarfeedâ€,
or, if the calendarfeed function is in the portfolio_controller:
events: “/portfolio/calendarfeedâ€,
Cheers.
oops I am sorry I posted the wrong ctp code actually it is
$(document).ready(function() {
$(‘#calendar’).fullCalendar({
events: ‘http://local.release-6-5.com/portfolios/calendarfeed’
});
});
“$this->set(‘events’, json_encode($rows));” this was just to check if the calendar picks up the events and it actually does pick up the events if I use
$this->set(‘events’, json_encode($rows)); in controller and events: in ctp file. but I need to use the feed url
“”
@Arry what is the name of your ctp file?
calendarfeed.ctp
@Arry you created a loop. You can’t have the view and the feed have the same name. Change the name of the feed to be something else.
I changed it to allcalendarfeed.ctp but still no luck. now how the function calendarfeed will call this view. I am not sure about this.
@Arry-
In portfolios_controller – you need to functions:
1. calendar- this will set up everything you need to display the calendar, except for the events.
2. calendarfeed – this is the function you posted here before, which echos the json feed at the end.
In your /views/portfolios – you only need one view.
This is the view you posted before. call it calendar.ctp (it has to have the same name you gave the function in 1.) and inside, in the fullcalendar configuration, make the events: item call your other function – calendarfeed. (or whichever name you want to call it)
do not write a ctp file for calendarfeed.
Hey, Thanks a tonnnnnnnnn…. it worked like charm…
I owe you one buddy… anytime anyday I will be there for you….
Three cheers for you….
@Arry – happy to help
good luck!