Path 如何在Zf2中重用服务层?

Path 如何在Zf2中重用服务层?,path,zend-framework2,service-layer,Path,Zend Framework2,Service Layer,我的a模块有一个服务层。现在我想通过从另一个模块B调用它来重用服务层。我知道我必须将我的服务层注入模块B的控制器中 我的问题是如何给出模块B的服务层路径 在模块B的控制器工厂中,$serviceLocator->get('A\Service\AService'),并在控制器中使用A\Service\AService;这些将不起作用,因为这些是模块A的目录位置。而且,默认路径似乎从控制器外部的文件夹开始。我如何为B使用这些路径 class IndexController extends Abstr

我的a模块有一个服务层。现在我想通过从另一个模块B调用它来重用服务层。我知道我必须将我的服务层注入模块B的控制器中

我的问题是如何给出模块B的服务层路径

在模块B的控制器工厂中,$serviceLocator->get('A\Service\AService'),并在控制器中使用A\Service\AService;这些将不起作用,因为这些是模块A的目录位置。而且,默认路径似乎从控制器外部的文件夹开始。我如何为B使用这些路径

class IndexController extends AbstractActionController
{
    function __construct(AService $sm) {

    $this->sm =$sm;
    }

    public function indexAction()
    {

            $event = $this->getEvent();
            $request = $event->getRequest();
            $router = $event->getRouter();
            $uri = $router->getRequestUri();
            $baseUrl = sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $request->getBaseUrl());

    $this->sm->callMethod($baseUrl);

    }
}
My module.config.php

return array(

 'controllers' => array(
        'factories' => array(
            'Application\Controller\IndexController' => 'Application\Controller\IndexControllerFactory',
        ),
    ),

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),
);
在module.php中

    public function getControllerConfig()
{
return array(
    'factories' => array(
        'Application\Controller\Index' => function ($sm){
            $a = $sm->getServiceLocator()->get('A\Service\AService');

            return new Application\IndexController($a);
        }
    ),
);
}

它的工作原理应该与在控制器中使用服务相同

我假设您在模块A中注册了服务

...
'invokables'   => array(
    'A\Service\AService' => 'A\Service\AService',
)
...
从那时起,您可以在任何模块中使用它(例如,在您的B modulemodule.php):


代码应该可以工作,所以你能复制粘贴它吗?请检查你答案的注释。我还添加了一些代码。我按照您的建议进行了更改。我得到错误“参数1传递给Application\Controller\IndexController::\uu construct()必须是\Service\AService的实例”。我为我的IndexController添加了代码。任何帮助都将不胜感激。谢谢错误是自我解释的。请。从module config复制粘贴代码的一部分,将实例传递给控制器的构造函数。呃,我指的是我建议的唯一一个执行“getControllerConfig()”的部分??您的module.config.php乱七八糟。只需删除controllers=>factory和controllers=>invokables。您正在模块中执行此操作。phpIt工作正常。谢谢!:)只是一个初学者,尝试了不同的东西,却忘了改变它。
public function getControllerConfig()
{
    return array(
        'factories' => array(
            'B\Controller\Index' => function ($sm){
                $a = $sm->getServiceLocator()->get('A\Service\AService');

                return new B\IndexController($a);
            }
        ),
    );
}