0

I have a Service Layer for my A module. Now I want to reuse the Service Layer by calling it from another module B. I understand that I have to inject my Service Layer in Controller of Module B.

My question is how do I give the path of the Service Layer for module B?

In my Controller Factory for module B, $serviceLocator->get('A\Service\AService'), and in my Controller, use A\Service\AService; these won't work as these are directory locations for module A.Also, It seems like default path starts from folder outside controllers. How do I use these path for 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(
        ),
    ),
),
);

In 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);
        }
    ),
);
}
user2740957
  • 171
  • 2
  • 15

1 Answers1

1

It should works the same as using the Service in A controller.

I assume that you register service in module A

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

From that point you can use it in any module (eg. in your B modul 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);
            }
        ),
    );
}
tasmaniski
  • 4,767
  • 3
  • 33
  • 65
  • I made the changes as you suggested. I get the error 'Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of A\Service\AService'. I added the code for my IndexController. Any help will be much appreciated. Thanks! – user2740957 Jul 07 '14 at 06:34
  • Error is self explained. Pls. copy-paste part of a code from module config, where you pass instance to controller's constructor. – tasmaniski Jul 07 '14 at 07:54
  • Ugh, I mean only part where you did "getControllerConfig()" as I suggest you?? – tasmaniski Jul 07 '14 at 08:10
  • Your module.config.php is in mess. Just remove controllers => factory and controllers => invokables. You are doing that in the Module.php – tasmaniski Jul 07 '14 at 08:25
  • It worked. Thanks! :) Just a beginner, tried out different things and forgot to change it. – user2740957 Jul 07 '14 at 08:37