我正试图用soap公开一些数据。
这是我的控制器拿着服务器(这里一切正常):
命名空间Application''Controller;
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,它运行得很好。为什么我不能在这里这样做?
[编辑]好吧,我尝试了其他方法,我在ParcoursController中创建了一个get函数,并将该函数调用到ExportClass中,而不是在ExportClass中使用EntityManager。我的ParcoursController已经在使用EntityManager将我的数据显示到页面中,因此它应该可以工作。但结果是一样的。我似乎应该以某种方式通过SOAP服务传递我的serviceLocator。我认为这不是个好主意。
好的,我搞定了。
这是我的工作会议,希望它能帮助到别人。
以上示例中的所有更改均为:
A: 将此添加到module.php中(ExportModel是ExportClass,来自上个示例,我刚刚更改了名称和命名空间)
return array(
'invokables' => array(
'Application'Model'ExportModel' => 'Application'Model'ExportModel',
),
)
B: 我把安装好的模型交给了我的SoapServer
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();
仅此而已。