首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >zend框架2插件查询

zend框架2插件查询
EN

Stack Overflow用户
提问于 2014-10-30 06:40:56
回答 1查看 69关注 0票数 0

如何从zend框架插件中查询?我在创建插件时使用了以下代码:ZF2 MVC global function

下面是我插件中的当前代码:

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-30 07:21:04

您应该调用$this->getServiceLocator()->getServiceLocator();,以便获得主服务管理器。当您调用$this->getServiceLocator()时,只需获得插件管理器。您还应该实现ServiceLocatorAwareInterface,以便系统将插件管理器注入您的插件对象。总之,您的代码应该如下所示:

代码语言:javascript
复制
//...
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版本不支持特性,然后将以下代码添加到类中:

代码语言:javascript
复制
use Zend\ServiceManager\ServiceLocatorInterface

private $serviceLocator;

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

public function getServiceLocator()
{
    return $this->serviceLocator;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26646599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档