Thursday, December 27, 2012

Zend_File_Transfer_Adapter_Abstract bug when file not uploaded

I'm not sure where goes wrong, but some how if a file element does not contain file,
the check returns empty.
there is a bug open since may, but yet no patch for it.

file effected is
line #617: public function isValid($files = null)

Tuesday, December 25, 2012

Zend framework 2 FormRow Render element with ID differently

Not sure what goes up with their development team that there render element with id attribute differently, compared to those without it.
Took me sometime to figure this, sigh*!

And guess what, i cant file a zend framework 2 bug.. lol
Only Zend Framework 1 jira tracker is available, not sure if ive visited the wrong link

Zend AbstractTableGateway does not support table alias

Couldnt figure out how to do it,
finally, i created my own Sql and and Select on my own.

But ive to overwrite executeSelect to allow table select with alias
$resultSet = $this->selectWith($oSelect);

Zend framework 2 route example

router' => array(
    'routes' => array(
      'bannermgt' => array(
        'type' => 'segment',
        'options' => array(
          'route' => '/bannermgt[/:action][/:id][/page/:page][/searchname/:searchname][/searchpath:searchpath][/]',
          'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id' => '[0-9]+',
            'page' => '[0-9]+',
            'searchname' => '[^/]*',
            'searchpath' => '[^/]*',
          'defaults' => array(
            'controller' => 'BannerMgt',
            'action' => 'index',
            "page" => 1,
            "searchname" => "",
            "searchpath" => ""

Take note that ordering of URL in route is strict.
Therefore searchname must come first before searchpath.

Or else , you may ommit:
And append as querystring

Zend Framework 2 redirect and forward

Both uses different path name to identify.
Redirect uses physical url on browser side, therefore the route name is to be used.

For forward wise, its using the factory service name to identify the controller to run.

'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Login' => 'Application\Controller\LoginController'

'router' => array(
        'routes' => array(
            "login" => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/login[/:action][/]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Login',
                        'action' => 'index',

$this->forward()->dispatch("Application\Controller\Index", array("action" => "someaction");

for redirect:
$this->redirect()->toRoute("login", array("action" => "someaction");

to get current controller name:

to get current route name:
return $this->getEvent()->getRouteMatch()->getMatchedRouteName();

Friday, December 21, 2012

Zend framework 2 get other view helper instance

in view:

$oFormat = $this->plugin("dateFormat");
echo $oFormat->__invoke(...);

to invoke directly:
echo $this->dateFormat(...);

Zend framework 2 Locale / i18n

In Application/config/module.config.php:

return array(
'translator' => array(
        'locale' => 'en_GB',
        'translation_file_patterns' => array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '',

In Application/Module.php bootstrap:

//set your time zone here

$oViewHelperManager = $e->getApplication()->getServiceManager()->get("ViewHelperManager");

To output in view template:
echo $this->translate("My text here");
echo $this->dateFormat(new DateTime(), IntlDateFormatter::SHORT);
echo $this->numberFormat(12345.0123, NumberFormatter::DECIMAL);
//default currency
echo $this->numberFormat(12345.5226, NumberFormatter::CURRENCY);

//custom currency output:
echo $this->currencyFormat(1234.5226, "MYR");

  • php5-intl

Thursday, December 20, 2012

Active directory login from Zend

After spending hours solving this issue,
and ive found that each different company have different active directory path setting.

For login purpose, the main concern are the baseDn.
Some uses this:

Some uses this:

And event this:
OU=Users,OU=.User Accounts,DC=EXAMPLE,DC=COM

Example of Zend 1 config in yaml:

      host: EXAMPLE.LOCAL
      accountDomainName: EXAMPLE.LOCAL
      accountDomainNameShort: EXAMPLE
      baseDn: OU=Users,OU=.User Accounts,DC=EXAMPLE,DC=LOCAL
      bindRequiresDn: false

Wednesday, December 19, 2012

Zend Framework 2 controller init


class MyController extends Controller {
protected $bInit = false;

public function onDispatch(\Zend\Mvc\MvcEvent $e) {
    if (! $this-> bInit) {
      $this-> bInit = true;
    return parent::onDispatch($e);

Zend Framework 2 file upload

Note: Remember to set destination and filter to adaptor before running isValid or Receive,
Because setting filter or destination after running isvalid will cause the isvalid to add 2 times check on the file, one as tmp_name and another as destination name..
which will cause Count validator to fail!

    $sDestination .= getcwd() . "/public/assets";
    if (!is_dir($sDestination)) {
      if (!@mkdir($sDestination, 0777, true)) {
        throw new \Exception("Unable to create destination: " . $sDestination);
    $size = new Size(array('min' => 10, "max" => 10000000)); //minimum bytes filesize
    $count = new Count(array("min" => 0, "max" => 1));
    $extension = new Extension(array("extension" => array("jpg", "png", "swf")));

    $oFile = $this->params()->fromFiles('txtFile1');
    $adapter = new \Zend\File\Transfer\Adapter\Http();
    $adapter->setValidators(array($size, $count, $extension), $oFile['name']);
    if (! $adapter->isValid()) {
      $dataError = $adapter->getMessages();
      $error = array();
      foreach ($dataError as $key => $row) {
        $error[] = $row;
      $form->setMessages(array('txtFile1' => $error));
    } else {
      if ($adapter->receive($oFile['name'])) {
      } else {
        $form->setMessages(array('txtFile1' => "Receive failed"));

Tuesday, December 18, 2012

Zend framework 2 and firebug and composer

To install firebug core lib via composer onto zend framework 2,


 "firephp/firephp-core": "dev-master"


"require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "firephp/firephp-core": "dev-master"

#run in console:
php composer.phar install

#for update on existing library
php composer.phar update

Kloxo dns template error

Ive came accross some long error on kloxo dns template,
and it turn out to be corrupted dns template.
I only realize this on creation of a domain or addition of a client.

running /script/fixdns seems to fix this issue,

if you have created a domain or client, just delete the client,
and recreate again.

yes, its still annoying that kloxo always have update issue which corrupt some of the files,
most obvious was roundcube update

Monday, December 17, 2012

Zend 2 Service Locator

Zend 2 Service Locator seems to be replacing Zend 1 Zend_Registry

Here are some reference: