2014-04-23 35 views
0

我想用肥皂公开一些数据。Zend +肥皂服务器+原则,与serviceLocator /服务管理器错误

这里是我的控制器保存服务器(一切正常这里):

命名空间应用\控制器;

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Zend\Json\Json; 
use Zend\Soap\Server; 
use Zend\Soap\AutoDiscover; 

class ExportController extends AbstractActionController 
{ 
private $_options = array('soap_version' => SOAP_1_2); 
private $_URI  = '/export'; 
private $_WSDL_URI = '/export?wsdl'; 
private $wsdl; 

public function indexAction() { 

    if (isset($_GET['wsdl'])) { 
     $this->handleWSDL(); 
    } else { 
     $this->handleSOAP(); 
    } 

    return $this->getResponse(); 
} 

private function handleWSDL() { 
    $serverUrl = strtolower(dirname($_SERVER['SERVER_PROTOCOL']))."://".$_SERVER['HTTP_HOST'].":".$_SERVER['SERVER_PORT']."/Moving-BO/public"; 
    $autodiscover = new AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence()); 

    $autodiscover->setClass('Application\WebService\ExportClass') 
       ->setUri($serverUrl.$this->_URI) 
       ->setServiceName('MySoapService'); 
    $autodiscover->handle(); 
    $this->wsdl = $autodiscover->generate(); 
} 

private function handleSOAP() { 
    $serverUrl = strtolower(dirname($_SERVER['SERVER_PROTOCOL']))."://".$_SERVER['HTTP_HOST'].":".$_SERVER['SERVER_PORT']."/Moving-BO/public"; 
    $soap  = new Server($serverUrl.$this->_WSDL_URI, $this->_options); 

    $soap->setClass('Application\WebService\ExportClass'); 
    $soap->handle(); 
    } 

} 

那么这里就是我出口类:

namespace Application\WebService; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceManagerAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use Zend\ServiceManager\ServiceManagerInterface; 
use Doctrine\ORM\EntityManager; 
use Zend\Json\Json; 
use Parcours\Entity\Parcours; 

class ExportClass implements ServiceLocatorAwareInterface 
{ 


protected $em; 

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

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

public function setEntityManager(EntityManager $em) 
{ 
    $this->em = $em; 
} 

public function getEntityManager() 
{ 
    $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 
    return $this->em; 
} 


/** 
* Dit bonjour! 
* 
* 
* @return string 
*/ 
public function helloWorld(){ 
    return 'coucou'; 
} 

/** 
* Retourne le titre d'un parcours 
* 
* @param integer $id 
* @return array 
*/ 
public function getParcours($id){ 

    $parcours = $this->getEntityManager()->getRepository('Parcours\Entity\Parcours')->findOneBy(array('id'=>$id)); 

    return $parcours->toArray(); 
} 

} 

我也有一个测试客户端,第一个函数:的helloWorld()工作正常,但第二个:getParcours($ ID )返回以下错误:

Call to a member function get() on a non-object 

它接缝像getServiceLocator()返回null。我使用了一个类似于AbstractActionController的代码片段:ParcoursController,它工作得很好。为什么我不能在这里做到这一点?

好的我已经尝试了其他的东西,而不是在ExportClass中使用EntityManager我在我的ParcoursController中做了一个get函数,并将此函数调用到ExportClass中。我的ParcoursController已经在使用EntityManager将我的数据显示到页面中,因此它应该可以工作。但结果是一样的。 看来我应该以某种方式通过我的serviceLocator通过SOAP服务。我不认为这是个好主意。

回答

0

好棒我钉了它。

这里是我的工作conf,希望它有助于某人。

从上面的例子所有变化:

答:已将此添加module.php(ExportModel是最后一个例子ExportClass我只是改变了名称和命名空间)

return array(
     'invokables' => array(
      'Application\Model\ExportModel' => 'Application\Model\ExportModel', 
     ), 
) 

B:我给instanciated模型到我的肥皂服务器

private function handleSOAP() { 
    $exportModel = $this->getServiceLocator()->get('Application\Model\ExportModel'); 
    $serverUrl = strtolower(dirname($_SERVER['SERVER_PROTOCOL']))."://".$_SERVER['HTTP_HOST'].":".$_SERVER['SERVER_PORT']."/Moving-BO/public"; 
    $soap  = new Server($serverUrl.$this->_WSDL_URI, $this->_options); 

    $soap->setClass('Application\Model\ExportModel'); 
    $soap->setObject($exportModel); 
    $soap->handle(); 

就是这样。