I'm trying to create a action helper for creating a menu. But I don't know how why it doesn't know the function isAllowed. I get the following error message:
Call to a member function isAllowed() on a non-object
<?php
class Zend_Controller_Action_Helper_Menu extends Zend_Controller_Action_Helper_Abstract
{
private $_acl;
public function createSubMenu($request,$identity){
$controller = $request->getControllerName();
$action = $request->getActionName();
$identity = $identity->user_role;
$access = $this->hasAccess($identity,$action,$controller);
$return;
$return .= "<ul>";
$return .= "<li><a href=''>".$identity."</a></li>";
$return .= "</ul>";
return $access;
}
private function hasAccess($role, $action, $controller)
{
$this->_acl = new Zend_Acl();
if (!$this->_acl) {
$this->_acl = Zend_Controller_Front::getInstance()->getPlugin('Acl');
}
return $this->_acl->isAllowed($role, $controller, $action);
}
}
I'm no Zend expert, but:
$this->_acl = new Zend_Acl();
- this will create new ACL object without any roles set, so it is useless (maybe you wanted to do this other way around - first get plugin, and if there isn't one create new?)->getPlugin('Acl');
- You should use full plugin class nameI usually create and initiate ACL object in plugin (on preDispatch
) and save it in Zend_Registry so i can later use Zend_Registry::get('Zend_Acl')->isAllowed($role, $resource, $privilege);