Today was the day I got tired of constantly disabling the layout and viewRenderer when making ajax calls in the ZF project I’m currently working on.
Today is the day I went and visited my old friend, the Zend Framework ContextSwitch Action Helper.
Tag: php
I was having trouble with some GD library functions not working in my PHP5.3 install on Ubuntu. Apparently Debian distributions do not compile PHP with the bundeled GD library, since it is a branch of the original GD, they consider it insecure.
To fix this problem I downloaded the sources and compiled PHP with the correct GD library.
The PHP community has been on a roll lately, after the great new release of PHP5.3 last year, a shiny new alpha is seeing light aka PHP5.4. The featurelist is not as big as the one of the previous release. But that’s because the previous one was huge… sporting stuff like namespaces, anonymous functions and late static binding.
PHP5.4 is mostly aimed at improving the previous release, but also gives us devs some nice features to enhance our codebase.
Since my last post, I’ve had some time to reflect on my take on a ServiceLayer and do a little experimentation. This time it around it won’t be all talk and no action though, time to get your code-writing-mittens on. For the purpose of this post I have created a GitHub repository, as the series will progress (if it ever will of course) I should end up with a descent, flexible, robust implementation.
A few points are to be made clear before we get our hands dirty. I will only be using stable libraries, so no ZF2 for now, I’ll be sticking to ZF1.11. (Although Matthew Weier O’Phinney wrote a nice introduction about the new plugin broker architecture in ZF2 which will come in handy in situations like this…). Luckily the autoloader rewrite in ZF 1.10 will allow us to use PHP 5.3 namespaces in our library classes. Another new PHP feature that will come in handy will be Late Static Binding.
Last week I’ve had some trouble unit testing a function that converted a point on a map given in degrees, minutes and seconds, to a longitude and a latitude. I did not write the function myself, but was only unit testing it. PHPUnit gave me some weird output, from which I could not extract a cause. Some mighty debugging got me to the bottom of it.
I’ve been thinking about robust, extensible, and easy patterns or strategies for setting up an application. Starting from an MVC viewpoint, I feel there’s a need for an extra layer, a service layer.
The service layer can handle workflow that only represents business logic. This meas the Controllers and Models can focus on their own tasks, being handling request logic and persistence logic respectively.
Service layer is a broad concept, and there are many ideas and examples available, for different languages and frameworks. My goal is to try and sculpt it in such a way that it fits in with PHP and Zend Framework.
Trying to get Xdebug working to enable PHPUnit’s code coverage ability. Here’s how I got it working:
Download the Xdebug dll from http://www.xdebug.org/download.php, you can find the information about which compiler was used and wether thread safety was enabled in phpinfo().
In theory you could place this dll anywhere on your system, since you’re installing it as a zend_extension, you are obligated to provide the full path. Albeit it is probably best to put it somewhere in the Zend Server installation directory. I myself put it in the default, which you can probably find at the end of your php.ini file, under the [Zend] section, in the extension_dir directive. But even if you put it in that directory, you will still have to give it the full path later on. The extension dir only refers to regular extensions, not zend_extensions.
For configuring the setup I recommend going through the ini files themselves, as the ZendServer’s webinterface gave me some problems, as to setting the directives in the php.ini to extension instead of zend_extension.
Add the following to the bottom of the [Zend] section in the php.ini, with the correct path ofcource:
zend_extension="C:\Path\to\Zend\ZendServer\lib\phpext\php_xdebug-2.1.0-5.3-vc9-nts.dll"
In order for this to work correctly we need to disable the Zend Debugger and Optimizer (for some reason…). Go to Zend\ZendServer\etc\cfg\debugger.ini and delete the following line:
zend_extension_manager.dir.debugger="C:\path\to\Zend\ZendServer\lib\debugger"
in the optimizerplus.ini file, in the same dir, delete and change the following lines:
; delete or comment this one zend_extension_manager.dir.optimizerplus="C:\path\to\Zend\ZendServer\lib\optimizerplus" ; change this one's value to 0 zend_optimizerplus.enable=0
Restart apache (or just php will do) and check you phpinfo() if it has an xdebug section.
I’ve noticed a lot of techniques to handle localized form valaidation messages. Some even require hacking core Cake classes. Lots of people are still wondering on how to set it up as easy as possible. And still the answers available in the cookbook are allthough correct, not the best or smartest way to implement this. Some time ago I came across a nice way how to get this working. The only thing you need to do is overwrite a model function.
Assuming that L10n is properly set up.
When a field does not pass a validation rule, the following function is called.
Model::invalidate($field, $value = true);
This message takes as a first parameter the model field which didn’t pass validation, and the error message as $value. The only thing to do is thus translate that message, this can easily be done by overwriting the method. notice the function __().
//in your AppModel
function invalidate($field, $value = true)
{
return parent::invalidate($field, __($value, true));
}
Now that our function is properly setup you can easily change the error messages for the fields in your models.
var $validate = array(
'username' => array(
'rule' => 'isUnique',
'message' => 'FIELD_UNIQUE'
)
);
In your .po file you can then add a msgid "FIELD_UNIQUE" and it is translated automaticly by the L10n class.


