Category Archives : Zend Framework


Zend_Cache_Core is missing the setCacheDir method

This seems to be troubling a lot of people, so after debugging Zend’s code, I’ll post the solution on how to change the cache directory of zend_cache.

I’m using the below code in my zend framework bootstrap to setup Zend_Cache for caching to files.

$frontend= array('lifetime' => 7200,
				 'automatic_serialization' => true);
$backend= array('cache_dir' => DIR_CACHE);
$cache = Zend_Cache::factory('core',
								   'File',
								   $frontend,
								   $backend);
//assign to registry                        
$registry     = Zend_Registry::getInstance();
$registry->cache = $cache;

I need to be able to override the cache directory at a later time and when I’m ready to use the cache in the application, I reference this Zend_Cache like below, which returns an object of Zend_Cache_Core, which you cannot call setCacheDir() on. We must first get the backend component, then call the method as below.

$cache = Zend_Registry::get('cache');
 
//set the new cache directory
$cache->getBackend()->setCacheDir($cacheDirectory);

How to insert an event onto secondary calendar using the google calendar api 2

With Zend Framework, I used their Gdata library to interface with the Google api. My application will be managing three separate calendars with the same Google account. Zend’s documentation does not tell you how to add an event to a calendar other than the default primary calendar. It’s not obvious with Google’s own API documentation either.

After some digging, I found the Zend_Gdata_Calendar::insertEvent() method accept a second optional parameter called $uri. There aren’t any comments or hints as to what this uri is for, but it is actually where you specify the calendar you’d like your event added to. How do you get the uri for your calendars? You need to query the api to get the information like below. Set your Google calendar login info in the below code and it will print your calendar uri’s to the screen. Use these uri’s as the second parameter to insertEvent().

$client = Zend_Gdata_ClientLogin::getHttpClient(GOOGLE_CALENDAR_USERNAME, GOOGLE_CALENDAR_PASSWORD, Zend_Gdata_Calendar::AUTH_SERVICE_NAME);
 
// Create an instance of the Calendar service
$service = new Zend_Gdata_Calendar($client);
 
try {
	$listFeed= $service->getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
	echo "Error: " . $e->getMessage();
}
 
foreach ($listFeed as $calendar) {
	echo '<br>calendar title: ' . $calendar->title->text;
	echo '<br>calendar uri: ' . $calendar->content->src;
}

Zend_Form Recaptcha doesn’t work with SSL 1

I was able to get a recaptcha working on my Zend Framwork application, but as soon as I added the “‘ssl’ => true” to my zend_form, the captcha disappeared. Here is the code used in zend_form:

//captcha
$this->addElement('captcha', 'captcha', array(
	'label' => 'Please Type:',
	'required' => true,
	'captcha' => array(
		'pubkey' => RECAPTCHA_PUBLIC_KEY,
		'privkey' => RECAPTCHA_PRIVATE_KEY,
		'captcha' => 'reCaptcha',
		'ssl'	=> true
	)
));

The reason ssl didn’t work with recaptcha is because the secure URL was out of date in my zend framework library. To update, open /Zend/Service/ReCaptcha.php and update the server constants at the top to

/**
 * URI to the regular API
 *
 * @var string
 */
const API_SERVER = 'http://www.google.com/recaptcha/api';
 
/**
 * URI to the secure API
 *
 * @var string
 */
const API_SECURE_SERVER = 'https://www.google.com/recaptcha/api';
 
/**
 * URI to the verify server
 *
 * @var string
 */
const VERIFY_SERVER = 'http://www.google.com/recaptcha/api/verify';

Using recaptcha with Zend_Form

I was pleasantly surprised at how simple it was to integrate a good captcha solution with Zend_Form. Your form doesn’t have to extended a special object or anything; you just need to add a “captcha” element like so:

$this->addElement('captcha', 'captcha', array(
	'label' => 'Please Type:',
	'required' => true,
	'captcha' => array(
		'pubkey' => RECAPTCHA_PUBLIC_KEY,
		'privkey' => RECAPTCHA_PRIVATE_KEY,
		'captcha' => 'reCaptcha'
	)
));

Be sure to change or set the defined variables for public and private keys provided to you with your recaptcha account. You don’t have to do anything to validate if the user entered the correct letters in your controller as Zend has already taken care of that part for you. As long as you’re using Zend’s isValid() method like so:

if ($form->isValid($request->getPost())) {