2014-10-30 58 views
0

我怎樣才能從zend框架插件查詢? ZF2 MVC global functionzend框架2從插件查詢

這裏是我當前的代碼在我的插件: 我創建插件使用此代碼

namespace Users\Controller\Plugin; 
use Zend\Mvc\Controller\Plugin\AbstractPlugin; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\Session\Container as SessionContainer; 
use Zend\Session\Container; 
use Zend\Db\Adapter\Adapter; 

class MenuPlugin extends AbstractPlugin 
{ 
    protected $permissionsTable; 

    public function availableLinks($theController = '', $modules = '') 
    { 
     $moduleId  = 0; 
     foreach ($modules as $row) { 
      $moduleId = $row->id; 
     } 
     $session_user = new Container('user'); 
     $roleId   = $session_user->user_data['roleId']; 
     $the_functions = $this->getPermissionsTable()->getAvailableFunctions($moduleId,$roleId); 
     return $the_functions; 
    } 

    public function getPermissionsTable() 
    { 
     if (!$this->permissionsTable) 
     { 
      $sm = $this->getServiceLocator(); 
      $this->permissionsTable = $sm->get('Users\Model\PermissionsTable'); 
     } 
     return $this->permissionsTable; 
    } 
} 

回答

0

你應該叫$這個 - > getServiceLocator() - > getServiceLocator();以獲得主要的服務經理。當你調用$ this-> getServiceLocator()時,你只需要插件管理器。並且您還應該實現ServiceLocatorAwareInterface,以便系統將插件管理器注入到您的插件對象中。總而言之,你的代碼應該是這個樣子:

//... 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorAwareTrait; 

class MenuPlugin extends AbstractPlugin implements ServiceLocatorAwareInterface 
{ 

    use ServiceLocatorAwareTrait; 

    protected $permissionsTable; 

    public function availableLinks($theController = '', $modules = '') 
    { 
     $moduleId  = 0; 
     foreach ($modules as $row) { 
      $moduleId = $row->id; 
     } 
     $session_user = new Container('user'); 
     $roleId   = $session_user->user_data['roleId']; 
     $the_functions = $this->getPermissionsTable()->getAvailableFunctions($moduleId,$roleId); 
     return $the_functions; 
    } 

    public function getPermissionsTable() 
    { 
     if (!$this->permissionsTable) 
     { 
      $sm = $this->getServiceLocator()->getServiceLocator(); 
      $this->permissionsTable = $sm->get('Users\Model\PermissionsTable'); 
     } 
     return $this->permissionsTable; 
    } 
} 

不過,我自己的PHP版本不支持的特徵,然後將下面的代碼添加到類:

use Zend\ServiceManager\ServiceLocatorInterface 

private $serviceLocator; 

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
{ 
    $this->serviceLocator = $serviceLocator; 
} 

public function getServiceLocator() 
{ 
    return $this->serviceLocator; 
} 
+0

目前仍然是一個錯誤,我的應用程序返回錯誤500 – jeck120591 2014-10-30 07:43:35

+0

您可以檢查您的錯誤日誌並提供更具體的錯誤信息嗎? – 2014-10-30 07:53:33

+0

它說: 致命錯誤:在/webapps/JerickAlita/new_cms/module/Users/src/Users/Controller/Plugin/MuenPlugin.php找不到接口'Users \ Controller \ Plugin \ ServiceLocatorAwareInterface' – jeck120591 2014-10-30 08:34:49