Global config variable in Kohana
I’ve been recently on a project that is using Kohana PHP framework. I had no knowledge of it coming in but a framework is a framework.
It is pretty easy to get a grasp on especially if you are used to Object-Oriented-Programming and Model-View-Controller setups.
One hiccup I ran into was trying to get a global variable like a config variable for instance inside the controller. The documentation is lacking this answer and since Kohana isn’t as widely used as Cakephp or Zend there wasn’t much in the way of Stack Overflow type answers from Google either.
If you’ve looked inside the Kohana config folder you can see that you can create and extend config files. I really liked this where I can create a new file in the config folder for all my email settings and name the file email.php. Seems much more organized. Now inside that file you set all your settings to the $config array.
$config['app_email'] = 'letstalk@emailme.com';
That’s all pretty straight forward. But getting this variable inside a controller is a little different than what you would expect.
Kohana::config('email.app_email');
That looks pretty unorthodox once you’ve worked with other frameworks. Inside the static config() function, the parameter given needs a structure like:
Kohana::config('the_config_file_name.array_key_of_the_config_array');
I did finally find this answer on the web and still had to do some guessing to get it to work but hopefully this will help kohana users find this info sooner then I did.
Using sys_get_temp_dir() in PHP < 5.2.1
I wanted to use the PHPExcel package on my Debian system running 5.2.0, however PHPExcel 1.7.1 requires the sys_get_temp_dir() function which doesn’t exist prior to PHP 5.2.1. Of course aside from updating PHP, we would need to replicate the functionality of sys_get_temp_dir(). This gem in the php.net comments on the function provides a workaround.
if ( !function_exists('sys_get_temp_dir'))
{
function sys_get_temp_dir()
{
if( $temp=getenv('TMP') ) return $temp;
if( $temp=getenv('TEMP') ) return $temp;
if( $temp=getenv('TMPDIR') ) return $temp;
$temp=tempnam(__FILE__,'');
if (file_exists($temp))
{
unlink($temp);
return dirname($temp);
}
return null;
}
}
How To pass an Array in the Querystring Using PHP
I recently ran across a silly WordPress situation where I needed to pass an array in the querystring. Here’s what I came up with:
$array = array('a' => 1, 'b' => 2, 'c' => 3);
$array = rawurlencode(serialize($array));
echo 'Pass array';
Then on the receiving getarray.php:
$array = unserialize(stripslashes($_GET['error_array']));
Enumerate Gmail Contacts
Here’s some sample code I’ve piled together to list gmail contacts:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');
$user = 'user@gmail.com';
$pass = 'password';
// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/'.$user.'/full');
$query->setMaxResults(100);
$feed = $gdata->getFeed($query);
$xml = new SimpleXMLElement($feed->getXML());
$entries = $xml->children('http://www.w3.org/2005/Atom');
foreach ($entries->entry as $entry )
{
$defaults = $entry->children('http://schemas.google.com/g/2005');
echo 'title: '. $entry->title."\n";
if(isset($defaults->email))
{
echo ' email: '.$defaults->email->attributes()->address."\n";
}
echo ' address: '.$defaults->postalAddress."\n";
echo "\n";
}
Highrise API Wrapper
Since Soocial no longer syncs with highrise, I’ve been looking for another solution to sync with my google apps. I’ve decided I’ll be writing my own, which I will release later.
In the mean time, I’ve found a Highrise API wrapper which should come in handy so I don’t have to deal with cURL as much.
include("push2Highrise.php");
$p = Push_Highrise();
$p->pushContact($_REQUEST);
$p->pushTask($_REQUEST);
$p->pushNote($_REQUEST);
$p->pushDeal($_REQUEST);
PHP self Keyword
The self keyword is one I hadn’t learned. It’s equivalent to using $this in a static context, so: self::callStaticMethod();.
I had classes where I was using Classname::getConn(); and having to change Classname every time I change classes. This allows me to do so without that change.