Wednesday, November 28, 2012

Zend Framework 2 Authentication using Acl + Activerecord

I have not written all the code, i have just modified the POLO'S version to make it work with Zendframework 2.0 stable release and phpActiveRecord and you can use this code with any other ORM if you wish. I have bundled the code with skeleton application rather then posting one by one file here. You can get the code from here


Thursday, November 8, 2012

Zend Framework 2 and ActiveRecord

I googled for adding php ActiveRecord in zend framework 2 but could not find it. However Arthur Bodera proposed the activerecord for zend framework 2 but it is not yet in Zend library. So i have decided to integrate standalone php ActiveRecord in zf2 as a third party library.

I have used zf2's tool to generated autoload classmap for activerecord that includes all ActiveRecord library files inside one file autoload_classmap.php. But activerecord has one file Utils.php, that include some functions outside class definition. The autoload classmap loader only find class methods not any function outside class, so i changed the Utils.php file and put all the outside functions into class and changed accordingly throughout the activerecord library. So if you wish to change the ActiveRecord library then please change Utils.php file accordingly.

You can find the complete code with zend framework 2 skeleton application here. Please put the zf2 library inside vendor directory. 

Thursday, January 19, 2012

Zend Flash message

For displaying flash message after certain event like, when user submit the page and need to show the success message in next request or in current request. For that Zend framework has a action helper FlashMessenger.

Here is a sample code that can used to flash the message.

Step 1: Add message to helper class in controller

$flash = $this->_helper->getHelper('FlashMessenger');
$flash->addMessage(array('success' => 'Posted message saved successfully' ));

Ste 2: Create View Helper to display message in layout

class Custom_View_Helper_FlashMessenger extends Zend_View_Helper_Abstract
{
public function flashMessenger ($width = null)
{
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

#for message
$message = array();

if($flash->getCurrentMessages())
{
foreach($flash->getCurrentMessages() as $key => $msg)
{
$message[key($msg)][] = $msg[key($msg)];
}

$flash->clearCurrentMessages();
}
else if($flash->getMessages())
{
foreach($flash->getMessages() as $key => $msg)
{
$message[key($msg)][] = $msg[key($msg)];
}
}

$str_msg = null;
$style = '';

if($width) $style = "style='width:".$width."px'";

if(count($message) > 0)
{
foreach($message as $key => $arr_msg)
{
$key = ($key == 'error')?'errormsg':$key;

$str_msg .= '
';
$str_msg .= '

';

foreach($arr_msg as $msg)
{
$str_msg .= rtrim($msg,'.'). "
";
}
$str_msg .= '

';
$str_msg .= '
';
}
}

return $str_msg;
}

public function hasMessage()
{
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

if($flash->hasMessages() || $flash->hasCurrentMessages())
return true;
else
return false;
}
}

Step 3: Display the message in layout




echo $this->flashMessenger();


Now you can add your falsh message in you any controller and it will display in you layout.

Hope this helps someone.