【问题标题】:How to reuse a Service Layer in Zf2?如何在 Zf2 中重用服务层?
【发布时间】:2014-07-04 05:59:44
【问题描述】:

我的 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);

    }
}

我的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);
        }
    ),
);
}

【问题讨论】:

  • 代码应该可以了,可以复制粘贴吗?
  • 请检查您的答案的评论。我也添加了一些代码。

标签: path zend-framework2 service-layer


【解决方案1】:

它应该与在 A 控制器中使用服务相同。

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

...
'invokables'   => array(
    'A\Service\AService' => 'A\Service\AService',
)
...

从那时起,您可以在任何模块中使用它(例如,在您的 B 模块 Module.php 中):

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

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

【讨论】:

  • 我按照您的建议进行了更改。我收到错误“传递给 Application\Controller\IndexController::__construct() 的参数 1 必须是 A\Service\AService 的实例”。我为我的 IndexController 添加了代码。任何帮助都感激不尽。谢谢!
  • 错误是自我解释的。请。从模块配置中复制粘贴部分代码,将实例传递给控制器​​的构造函数。
  • 呃,我的意思是您按照我的建议执行“getControllerConfig()”的部分??
  • 你的 module.config.php 乱七八糟。只需删除控制器 => 工厂和控制器 => 可调用对象。您在 Module.php 中执行此操作
  • 成功了。谢谢! :) 只是一个初学者,尝试了不同的东西,却忘了改变它。
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多