ZendFramework 2 - AbstractFactoryInterface的问题; Param不包含路由/控制器命名空间

时间:2017-07-25 12:50:38

标签: zend-framework2

我有一个带有一条路线和一个抽象控制器工厂的模块。

路线

namespace BaseApi;

use BaseApi\Factory\Controller\AbstractApiControllerFactory;
use Zend\Mvc\Router\Http\Segment;

return [
    'router' => [
        'routes' => [

            'v2a-api' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/api/:controller',
                    'defaults' => [
                        '__NAMESPACE__' => 'BaseApi\Controller',
                    ],
                ],

            ],
        ],
    ],
    'controllers' => [
        'abstract_factories' => [
            AbstractApiControllerFactory::class,
        ],
    ],
];

抽象控制器工厂     

namespace BaseApi\Factory\Controller;

use BaseApi\Controller\ApiController;
use BaseApi\Exception\ApiAbstractControllerFactoryException;
use Zend\Filter\Inflector;
use Zend\Filter\StringToLower;
use Zend\Filter\Word\CamelCaseToDash;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Class AbstractApiControllerFactory
 *
 * @package BaseApi\Factory\Controller
 */
class AbstractApiControllerFactory implements AbstractFactoryInterface
{


    /**
     * @param \Zend\ServiceManager\ServiceLocatorInterface $controllerManager
     * @param $name
     * @param $requestedName
     *
     * @return bool
     */
    public function canCreateServiceWithName(ServiceLocatorInterface $controllerManager, $name, $requestedName)
    {
        dump('api', $name, $requestedName);
        $serviceManager = $controllerManager->getServiceLocator();

        $configKey = $this->getConfigKey($requestedName);

        $config = $serviceManager->get('Config');

        return (
            strpos($requestedName, 'BaseApi\Controller') === 0
            && key_exists(
                $configKey,
                !empty($config['d3-api']) ? $config['d3-api'] : []
            )
        );
    }

    /**
     * @param \Zend\ServiceManager\ServiceLocatorInterface $controllerManager
     * @param $name
     * @param $requestedName
     *
     * @return \BaseApi\Controller\ApiController
     * @throws \BaseApi\Exception\ApiAbstractControllerFactoryException
     */
    public function createServiceWithName(ServiceLocatorInterface $controllerManager, $name, $requestedName)
    {
        $serviceManager = $controllerManager->getServiceLocator();

        $configKey = $this->getConfigKey($requestedName);

        $config = $serviceManager->get('Config');

        if (empty($config['d3-api'][$configKey]['service'])) {
            throw ApiAbstractControllerFactoryException::missingServiceKey($configKey);
        }
        if (!$serviceManager->has($config['d3-api'][$configKey]['service'])) {
            throw ApiAbstractControllerFactoryException::missingServiceException(
                $config['d3-api'][$configKey]['service']
            );
        }

        return new ApiController(
            $serviceManager->get($config['d3-api'][$configKey]['service'])
        );
    }

    /**
     * Transforms Controller-Name + Namespace $requestedName in the related Config-Key
     * :: BaseApi\Controller\GameLists => game-lists
     *
     * @param $requestedName
     *
     * @return string
     */
    protected function getConfigKey($requestedName)
    {
        $controllerName = substr($requestedName, strrpos($requestedName, '\\') + 1);

        $inflector = new Inflector(':classname');

        $inflector->setFilterRule(
            ':classname',
            [
                CamelCaseToDash::class,
                StringToLower::class,
            ]
        );

        return $inflector->filter(['classname' => $controllerName]);
    }
}

附加配置(对于此问题无关紧要,仅为了完整起见)

<?php

return [
    'd3-api' => [
        'customers' => [
            'service' => 'V2aCustomerDb\Service\Customers',
        ],
    ],
];

现在我的问题:我有2个ZF-MVC-Apps。在一个,一切正常;当我在/api/customers下开始请求时,我会看到我的对象。在另一个中,AbstractApiControllerFactory :: canCreateServiceWithName的参数$ requestedName不包含命名空间。我只收到customers而不是BaseApi\Controller\customers

项目设置是一样的。在composer上安装的所有内容都具有相同的版本。所以它必须是一个配置错误,但几个小时后,我不知道它可能是什么......

有人有什么建议吗?

1 个答案:

答案 0 :(得分:0)

两天后......

Module.php必须如下所示(添加BootstrapListenerInterface)

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace CustomerDatabase;

use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\Mvc\ModuleRouteListener;

/**
 * Class Module
 *
 * @package CustomerDatabase
 */
class Module implements BootstrapListenerInterface
{
    public function onBootstrap(EventInterface $event)
    {
        $eventManager = $event->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
    [...]
}