简体   繁体   English

ZF2:从外部类调用服务?

[英]ZF2 : Call a service from an external class?

In my Zend Framework 2 project, I have an external lib and I want to save my information in the base with the model. 在我的Zend Framework 2项目中,我有一个外部库,我想将信息与模型一起保存在基础中。

.... .... .... .... .... ....

EDITED MESSAGE : 编辑信息:

I explain again my need: In my controllers, I make insertions and deletions in the database and I want to log all actions in a "t_log" table . 我再次解释我的需要:在控制器中,我在数据库中进行了插入和删除操作,并且希望将所有操作记录在“ t_log”表中。 To do it, I have thought to create an external class. 为此,我考虑过要创建一个外部类。

My question is: How I can call my models method from my external class ? 我的问题是:如何从外部类调用我的模型方法?

namespace Mynamespace;

use Firewall\Model\Logs;
use Firewall\Model\LogsTable;

class StockLog
{
    public function addLog()
    {
       $log = $this->getServiceLocator()->get('Firewall\Model\LogTable');
       $log->save('user added'); 
       die('OK');
    }
}

My model : 我的模特:

namespace Firewall\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

class UserGroupTable
{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function save()
    {
        // How I Can call this method from the StockLog method ?
    }
}

Thanks you ! 谢谢 !

getServiceLocator is a function of \\Zend\\Mvc\\Controller\\AbstractActionController so it is supposed to be used in your controllers. getServiceLocator\\Zend\\Mvc\\Controller\\AbstractActionController的函数,因此应该在您的控制器中使用。

I dont know what your StockLog class is, but it is not extending any other class, so i guess it has not that function and your error is one step before, in the call to getSErviceLocator that is not defined, so its not returning an object. 我不知道您的StockLog类是什么,但是它没有扩展任何其他类,所以我想它没有该功能,并且您的错误是在getSErviceLocator的调用之前的第一步,因此它不返回对象。

Probably you can inject the service locator with something like 可能您可以向服务定位器注入类似

class StockLog 
{

  private $serviceLocator= null;

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


    public function add()
    {
       # Do you know how I can call the service ??
       $User = $this->serviceLocator->get('Firewall\Model\UserTable');
    }


}

and then, when you create your StockLog object, in your controller, you inject the servicelocator 然后在创建StockLog对象时,在控制器中注入servicelocator

public class yourController extends AbstractActionController {

   public function yourAction(){
      $mStockLog = new StockLog ();
       $mStockLog->setServiceLocator($this->getServiceLocator()); 
    /...

   }
}

Also, if you only need the 'Firewall\\Model\\UserTable' service, you should inject just that, instead of the serviceLocator. 另外,如果仅需要'Firewall\\Model\\UserTable'服务,则应仅注入该内容,而不要注入serviceLocator。

At any rate you should minimice the knowledge of your model classes about the rest of the system, hving always in mind the dependency inversion principle , to get a better decoupling 无论如何,您应尽量减少模型类对系统其余部分的了解,始终牢记依赖关系反转原理 ,以实现更好的去耦

UPDATE 更新

inject the log table 注入日志表

namespace Mynamespace;

use Firewall\Model\Logs; use Firewall\Model\LogsTable;

class StockLog {


 private $logTable= null;

  public function setLogTable($logTable)
  {
    $this->logTable= $logTable;
  }

public function addLog()
{

   $this->logTable->save('user added'); 
   die('OK');
}

} }

and then, when you create your StockLog (in your controller, or wherever you do it, before you use it) you inject the logtable object 然后,当您创建StockLog时(在您的控制器中,或者在执行此操作的任何位置,使用它之前),您都会注入logtable对象

  $mStockLog = new StockLog ();
  $mStockLog->setLogTable($this->getServiceLocator()->get('Firewall\Model\LogTable')); 

Of course, Im suposing that you configured correctly your Firewall\\Model\\LogTable class to be retrieved by means of the service manager, in getServiceConfig() in your Module.php 当然,我假设您已正确配置了Firewall\\Model\\LogTable类,以便通过服务管理器在Module.php中的getServiceConfig()中进行Module.php

  public function getServiceConfig() {
       return array (
            'factories' => array (
                    'Firewall\Model\LogTable' => function ($sm) {
                         $logTable = //create it as you use to  
                  return $logTable;
            }  

        )

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM