2014-04-23 32 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(); 

就是這樣。