douyi1779 2013-03-11 14:53
浏览 18
已采纳

如何从我自己的库中访问zend 2中的getServiceLocator

I have been developing a project in zend 1 but decided to move over to zend 2 to take advantages of things like events etc.

My initial problem is that I can't seem to find any tutorials on how to use models in the way I need to use them.

What I have is an Api controller which is routed to as /api/soap

this soap endpoint loads a class that has all the methods I want to expose via SOAP

namespace MyProject\Controller;

$view = new ViewModel();
$view->setTerminal(true);
$view->setTemplate('index');

$endpoint = new EndpointController();

 $server = new Server(
            null, array('uri' => 'http://api.infinity-mcm.co.uk/api/soap')
 );


$server->setObject($endpoint);

$server->handle();

and my controller that contains all the functions is

namespace MyProject\Controller;
class EndpointController
{

    public function addSimpleProducts($products)
    {

    }

}

Now what I want to be able to do is access my products model from inside this EndpointController.

So i've tried this:

protected function getProductsTable()
{
    if (!$this->productsTable) {
        $sm = $this->getServiceLocator();
        $this->productsTable= $sm->get('MyProject\Model\ProductsTable');
    }
    return $this->productsTable;
}

When I run this I get the fatal error that EndpointController::getServiceLocator() is undefined.

I am very new to Zend 2 but in Zend 1 it feels like this would be a very minor step in my development and im getting to the point of sacking zend 2 off and going back to zend 1 or even switching to symfony 2 where its simple to use doctrine...

help?

  • 写回答

2条回答 默认 最新

  • duanlijia5864 2013-03-12 08:52
    关注

    If you want your controller to have access to the ServiceManager, then you need to inject a ServiceManager into it.

    Within the MVC system, this happens pretty much automatically for you as the ServiceManager is used to create the instance of the Controller. This is not happening for you as you are creating your EndpointController using new.

    You either need to create this controller via the MVC or instantiate and configure your own ServiceManager instance and pass it to EndpointController.

    Alternatively, instantiate the dependencies, such as ProductTable and set them into your EndpointController.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料