如何从zend框架插件中查询?我在创建插件时使用了以下代码:ZF2 MVC global function
下面是我插件中的当前代码:
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;
}
}发布于 2014-10-30 07:21:04
您应该调用$this->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;
}https://stackoverflow.com/questions/26646599
复制相似问题