Lennaert Ekelmans - LennieZ 4 months, 1 week, 1 day to my birthday!

Five useful CakePHP functions I just dicovered

Jun 25th 2009, 13:33

I don't have to explain why CakePHP is such a brilliant PHP framework, it simply is. The good part with frameworks is that they include a rich reusable API library full of functions that will make life easier for most programmers. CakePHP is such a framework with an enormous rich library. I only know half of the functions but every day I discover some new great functions which some of them I want to share.

1. Httpsocket
After I wrote an article about how we built Twittermail in 48 hours, Matt from PseudoCoder.com wrote a blogpost about Httpsocket. I was constantly using the curl library from PHP, I just did not know CakePHP had it's own 'grabber' included. It's called Httpsocket, it's a core Utility library and it's quite interesting what the possibilities are. I truly love curl because they have many functions like handling cookies, auto follow header locations and other very handy stuff. But for quick calls it's just as easy to use the CakePHP Httpsocket. Here's an example:

To simply get my LastFM timeline, I only need: 

App::import('Core', 'HttpSocket'); 
$HttpSocket = new HttpSocket();
$lastFMxml= $HttpSocket->get('http://ws.audioscrobbler.com/1.0/user/LennieZ/recenttracks.xml');

 You can be much more specific with the Httpsocket library like giving a User Agent or supplying user authentication. See this page to get more info about that.

2. Convert an Object into an Array
For data I prefer array's more than objects. To get my LastFM timeline I need to retrieve an XML file. I always used simplexml_load_string to convert the results to an XML object. After that I had to convert the object to an array and therefore I used a custom function I found on php.net. Lately I discovered the beautiful 'Set' Core library from CakePHP. In the Set Core Utility library there are some very useful functions to manipulate arrays and objects. One of the functions is called Reverse, and this function is doing exactly what I want in just 1 line:

$result = Set::reverse($lastFMxml);


3. The XML library
Coming to the next point, the XML library. I did not know CakePHP had an XML core library and that's why I had to use 'simplexml_load_string'. With the XML library you can easily convert a string to an XML object.

$xml = new Xml($input);
$xml = Set::reverse($xml);


You can even get a remote XML file by using an URL!

$xml = new Xml('http://ws.audioscrobbler.com/1.0/user/LennieZ/recenttracks.xml');
$xml = Set::reverse($xml);


4. Cache::set()
I just love cache, I use it quite often and can't live without it. The only problem is that when I use Cache::write(), in the past I could always tell this function for how long it should be cached. For some reason this is not working anymore and you need to supply a config name. In my core.php file I told some configs for how long it should be cached. Today I found out I can tell the cache on the fly for how long it should be cached! Using Cache::set()!

Cache::set(array('duration' => '+30 days')); 
Cache::write('results', $data); 
// Later on 
Cache::set(array('duration' => '+30 days')); 
$results = Cache::read('results');


For some reason you also need to supply Cache::set when you read your data.

5. String insert
Most of my projects I prepare for Internationalization so it will be easy to add more languages in the future. Therefore I'm using the __('Hello!') function for every text I need. But what if you need data in your text like a name or an age. You can use sprintf and the %s signs in the text, but I just think that's not so nice in the PO files.

By using String insert you have a more accessible text in your PO files which is easier to understand for translators.

String::insert(__('My name is :name and I am :age years old.', true), array('name' => 'Bob', 'age' => '65'));

 
Next month I will talk about hacking, so stay tuned!

CakePHP: No markup with formhelper input

Apr 10th 2009, 11:30

Just a short blog post about CakePHP, I see loads of people around me who don't want any markup like DIVS or LABELS around their form input elements. There are plenty reasons you don't want those markup, for example when you need your form in a table.

I see programmers using $form->select(); or $form->radio(); to avoid all the markup, but there's a simple trick to hide all the additional elements and still benefit from all the magic features of $form->input();

Just add two parameters in the input options:

input('title', array(
    'div' => false,
    'label' => false)
);

 

How we built Twittermail.com in 48 hours

Mar 24th 2009, 01:00

Okay, for those who don't know Twittermail, Twittermail is an open idea by Boris Veldhuijzen van Zanten, he thought that it would be great if people could e-mail their tweets to Twitter.com.

This is potentially very useful for people who use mobile phones. Of course you can go to the mobile webpage of Twitter, but sending an e-mail is pretty much easier. Some older phones do not even contain a browser and only have e-mail functionality. Also a lot of businesses block Twitter.com on their corporate network but with Twittermail you can continually update your Twitter account even from your business e-mail address.

Those who register at Twittermail, get a secret unique email address like 1234abcde [AT] twittermail com. When you send an e-mail to this secret email address, it will get posted instantly to Twitter.com through the Twitter API.

Read the full article

Lennaert Ekelmans



Lennaert Ekelmans

I'm Lennaert Ekelmans, The Netherlands, 22 years old, serial entrepreneur, web developer, CakePHP'er, hobby-photographer and trance-addict.

Since April I've a blog, read here why.