Search code examples
phpsoapzend-framework

Zend action controller can't be called


I want to build a Zend-3-MVC application which can handle SOAP requests. It should therefore act as an SOAP server.

First of all I created the following controller:

<?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;

    }

}

And this is my router.global.php in config/autoload:

<?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,
                    ],
                ],
            ],
        ],
    ],
];

And now I make an SOAP GET request to

https://example.com/soap/wsdl

But the route can't be resolved. I expect that the wsdlAction method is called but I only get a 404.


Solution

  • You need to register your controller also.

    use Zend\ServiceManager\Factory\InvokableFactory;
    
    'controllers' => [
            'factories' => [
                MyProject\Controller\SoapController::class => InvokableFactory::class            
            ],
        ],
    

    So, now your code should be :

    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,
                            ],
                        ],
                    ],
                ],
            ],
        ];