Posts Tagged php

Generating XML and passing it to jQuery

I am generating some XML and then POSTing it to a jQuery based plugin. Looking at the POSTed XML in Firebug, everything appears normal. However, something is not getting passed to the plugin so it is using a amount (which results in no data being properly displayed). The solution turned out to be quite simple… set the Mime-Type on the posted XML! Doh!

So by adding

header('Content-Type: text/xml');

everything passed smoothly.

     

, ,

No Comments

Interesting Article about Caching Models in Zend Framework

Interesting article over at http://www.contentwithstyle.co.uk/content/a-caching-pattern-for-models about implementing caching with Zend Framework models. In the comments there is a good point about how to refresh such a cache with the author suggesting using something like a $this->cache->clear() to clear the cache upon adding or updating content.

     

, , ,

1 Comment

Zend_Tool Set-up

Download Zend Framework (1.8.0 is the current version at present… change as needed):

wget http://framework.zend.com/releases/ZendFramework-1.8.0/ZendFramework-1.8.0-minimal.tar.gz

Unpack the downloaded file:

tar zxvf ~/Downloads/ZendFramework-1.8.0-minimal.tar.gz

Move into that unpacked directory:

cd ~/Downloads/ZendFramework-1.8.0-minimal

Find out where your PHP binary is:

which php

Put zf.sh and zf.php into the same dir as the PHP binary:

cp bin/zf.sh bin/zf.php /Applications/MAMP/bin/php5/bin/

Find out your include path:

php -i | grep "include_path"

Put the contents of the library folder into that location:

cp -r library/* /Applications/MAMP/bin/php5/lib/php/

Test Zend_Tool:

zf.sh show version

For ease of use, let’s alias zf.sh to just zf. First open your bash login file:

vim ~/.bash_login

Add this line:

alias zf=zf.sh

You are now set up. Next stop, actually creating a project with Zend_Tool!

     

, , ,

No Comments

Extending Zend_View to Implement a Concrete Function…

…without using a view helper*.

In a nutshell:

  1. Extend Zend_View
  2. Put your method in this extended class
  3. Instantiate the class (in your bootstrap for instance)
  4. Assign it to the ViewRenderer
  5. Pass that viewrenderer to Zend_Controller_Action_HelperBroker’s addHelper method
  6. Use it in your view

The details:

In /library/My/Zend/ create View.php:

class My_Zend_View extends Zend_View
{
public function _($string)
{
return Zend_Registry::get('translate')->_($string);
}
}

In your bootstrap:

require_once APPLICATION_PATH.'../library/My/Zend/View.php';

$view = new My_Zend_View();

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

In your view:

echo $this->_("Hello");

Some extras. I have APPLICATION_PATH defined in index.php as:

define('APPLICATION_PATH', realpath(dirname(__FILE__)).'/application/');

and I have Zend_Translate instantiated in bootstrap.php like:

$translate = new Zend_Translate('csv', APPLICATION_PATH.'../library/Mine/source-en.csv', 'en');

and lastly, don’t forget to register $translate with the registry or that echo in the view file won’t work:

$registry->translate = $translate;

*Why not use a view helper? Well, you should, actually. The only reason for this solution is that the original need was for something that would work with the underscore notation used by Poedit. Apparently implementing a method named “_” doesn’t work in a helper so this solution extends Zend_View… adding an underscore method.

Sooo… if you should use a view helper what does that look like?
In /library/My/Helper/Translate.php

class My_Helper_Translate extends Zend_View_Helper_Abstract
{
public function translate($string)
{
return Zend_Registry::get('translate')->_($string);
}
}

In your bootstrap:

$view->addHelperPath("../library/My/Helper/", "My_Helper");

In the view:

echo $this->translate("Hello");
     

, ,

No Comments