Generating vCards with CakePHP using extensions

vCards are a nice added touch for an application that acts as a Address book, or contact management. vCards are easily used by most mail clients, and are a plain text standard making them nice and easy to implement. While hCard and other microformats are gaining popularity, the widespread support still doesn’t exist. Leaving vCard as the primary format to transfer contact information out of a web application

Enabling the VCF extension

In order to generate VCF output, you should first add RequestHandler to your components and enable the vcf extension with Router.

Show Plain Text
  1. Router::parseExtensions('vcf');

Like all other extensions, once enabled, RequestHandler will automatically switch our layout, and view files to those found in the appropriate sub-directories. One snag with vcf is that the mime-type information is not in RequestHandler so the headers aren’t set automatically. This is a quick fix for that, in you AppController::beforeFilter() or any beforeFilter() you can add the following to map the extension to the correct mime-type

Show Plain Text
  1. $this->RequestHandler->setContent('vcf', 'text/x-vcard');

Now all you have to do is generate vCard formatted data. Being the nice guy I am, I’ve added the vCard helper I’ve been using into my github for your enjoyment. It has a simple API, that allows you to set most vCard fields and generate the headers, footers and properly encoded values. I think the test cases pretty much explain its use. But if you have any questions let me know.

Comments

That’s a nice tip. Simply telling Router to parse the extension of an “unsupported” extension won’t do much at all. Adding a setContent() will get Cake to select the right layout.

I still don’t get Cake to select the correct view this way (at least not inside a plugin) which is strange.

(I am writing a question about that on the google group since this is hardly the best forum.)

Martin Westin on 3/20/09

On second thought… forget that about things not working…

It was a great tip, preiod.
I was just dumb and blind for a while.

Martin Westin on 3/20/09

Any way to import vcf into cake ??? ; an import contact function into my application ?

Sebastien G. on 4/6/09

Sebastien G.: I didn’t have the need to parse vcf’s into something else, so I didn’t write that code. However, if you have a site you have the perfect subject for an article or tutorial! :)

mark story on 4/7/09

Thanks for this, I’ve set it up and it’s working great!

I had a bit of trouble wrapping my brain around how exactly to put it to use, and although I’ve got it working, I have a nagging feeling that I’m not really doing it right, because there’s no controller (or probably more precisely, no action) involved. I followed the steps you described, ended up creating a layout and a view (as dictated by CakePHP’s error messages), and from there I got things running. But I had to do all of the processing that normally happens in the controller in the view.

The code in the test case helped me to assemble my output, but it might be more beneficial to have a fleshed-out example of how this should be implemented.

Regardless, I’ve got it working, a little weird though it might be, so thanks again!

room34 on 5/24/09

room34: Normally I would just have a /users/view/34.vcf which would generate a vCard for a particular user. So a separate view file is used to generate the vCard output. I’m of the opinion that formatting data should be done in the View anyways but I’m curious what processing needed to be done in the controller.

mark story on 5/31/09

Sebastien G. on 6/4/09

Any way to import vcf into cake ??? ; an import contact function into my application ?

Hello Sebastien G,

Have you found solution to import vCard(.vcf) in CakePHP.

Please help me.

Thanks in advance.

Rakshit Patel.

Rakshit on 7/25/09

Rakshit: just have a file upload form that accepts vcards, and then use php’s file reading/traversing functions to get the data you need. The best place to start would be a google search for ‘cakephp file uploads’ and then ‘php file functions’.

Interesting name.

adam on 9/23/09

Thanks for this! I have it installed working, I expanded the helper to allow for ‘notes’ and other labels via the __call method.

I do have a question though, how do you force the browser to download the .vcf file rather than display it as text?

Users can view the vcard by navigationg to contacts/view/#.vcf but it shows as text. If I view source its all basic text percicely as I’d want it, but I’d like to just force the browser to download the file. Is that possible?

Things I’ve tried:
I’ve tried creating a .htaccess file in the /views/contacts/vcf/ folder that contains “AddType text/x-vcard .vcf” to no avail. I didn’t expect that to work, so I tried moving the .htaccess file around hoping to find the sweetspot, but no luck.

Any advice?

Nick on 10/21/09

I found the answer.

All I had to do was set the header information in my view:

[code]
header("Content-disposition: attachment; filename=all_contacts.vcf");
header("Content-type: text/text/x-vcard");
?>
[/code]
And then proceed with the building of the vcard via the helper.

Hope that helps someone else,
Nick

Nick on 10/22/09

Typo. that Content-type should be text/x-vcard not text/text/x-vcard

Whops.

NIck on 10/22/09

For those asking about vCard parsing, take a look at this PEAR library:

http://pear.php.net/package/Contact_Vcard_Parse/docs/1.32.0/Contact_Vcard_Parse/Contact_Vcard_Parse.html

it works well for me.

Nick on 10/23/09

Comments are not open at this time.