无法调用Zend动作控制器

时间:2019-03-01 13:47:33

标签: php soap zend-framework

我想构建一个可以处理SOAP请求的Zend-3-MVC应用程序。因此,它应该充当SOAP服务器。

首先,我创建了以下控制器:

<?php

namespace MyProject\Controller;



use Zend\Mvc\Controller\AbstractActionController;

use Zend\Soap\AutoDiscover as WsdlAutoDiscover;

use Zend\Soap\Server as SoapServer;



class SoapController extends AbstractActionController

{

    public function wsdlAction()

    {

        $request = $this->getRequest();



        $wsdl = new WsdlAutoDiscover();

        $this->populateServer($wsdl);



        $response = $this->getResponse();

        $response->getHeaders()->addHeaderLine('Content-Type', 'application/wsdl+xml');

        $response->setContent($wsdl->toXml());



        return $response;

    }



    public function serverAction()

    {

        $request = $this->getRequest();



        $server = new SoapServer(

            $this->url()

                ->fromRoute('soap/wsdl', [], ['force_canonical' => true]),

            [

                'actor' => $this->url()

                    ->fromRoute('soap/server', [], ['force_canonical' => true]),

            ]

        );

        $server->setReturnResponse(true);

        $this->populateServer($server);



        $soapResponse = $server->handle($request->getContent());



        $response = $this->getResponse();

        $response->getHeaders()->addHeaderLine('Content-Type', 'application/soap+xml');

        $response->setContent($soapResponse);



        return $response;

    }

}

这是config / autoload中的router.global.php:

<?php
use Zend\Router\Http\Literal;

return [
    'router' => [
        'routes' => [
            'soap' => [
                'type' => Literal::class,
                'options' => [
                    'route' => '/soap',
                ],
                'may_terminate' => false,
                'child_routes' => [
                    'wsdl' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/wsdl',
                            'defaults' => [
                                'controller' => \MyProject\Controller\SoapController::class,
                                'action' => 'wsdl',
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                ],
            ],
        ],
    ],
];

现在我向发出SOAP GET请求

https://example.com/soap/wsdl

但是路线无法解决。我希望可以调用wsdlAction方法,但只能得到404。

1 个答案:

答案 0 :(得分:0)

您还需要注册您的控制器。

use Zend\ServiceManager\Factory\InvokableFactory;

'controllers' => [
        'factories' => [
            MyProject\Controller\SoapController::class => InvokableFactory::class            
        ],
    ],

所以,现在您的代码应该是:

use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;
    return [
        'controllers' => [
                'factories' => [
                    MyProject\Controller\SoapController::class => InvokableFactory::class            
                ],
            ],
        'router' => [
            'routes' => [
                'soap' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => '/soap',
                    ],
                    'may_terminate' => false,
                    'child_routes' => [
                        'wsdl' => [
                            'type' => Literal::class,
                            'options' => [
                                'route' => '/wsdl',
                                'defaults' => [
                                    'controller' => MyProject\Controller\SoapController::class,
                                    'action' => 'wsdl',
                                ],
                            ],
                            'may_terminate' => true,
                        ],
                    ],
                ],
            ],
        ],
    ];