Zend Framework 2 : Using Zend Framework 2 Module in Symfony 2 Project
In ZF2, Module completely become re-usable code, It can be called even without Zend Framework MVC stack. I will give you extreme example, I will call my ZF2 module that contains service called by Symfony 2 Framework. It’s very rare, but sometime, you don’t need to Reinvent Your created module, even it created in other environment.
For example, I have a ZF2 Module like the following :

Which has Service like the following :
a. Service Class ( ZFModule\Service\MySampleService.php )
namespace ZFModule\Service;
class MySampleService
{
protected $config;
public function getConfig()
{
return $this->config;
}
public function setConfig(array $config)
{
$this->config = $config;
}
}
b. The Service Factory ( ZFModule\Service\MySampleServiceFactory )
namespace ZFModule\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class MySampleServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
$service = new MySampleService;
$service->setConfig($config);
return $service;
}
}
c. Register MySampleService into ZF2 Service Manager
namespace ZFModule;
class Module
{
public function getConfig(){/*common code here */}
public function getAutoloaderConfig(){/*common code here*/}
public function getServiceConfig()
{
return array(
'factories' => array(
'MySampleService' => 'ZFModule\Service\MySampleServiceFactory'
),
);
}
}
Ok, let’s go step by step :
1. Copy the module to Your Symfony Project.

2. add require zendframework/zendframework to your composer.json
.....
require {
"php": ">=5.3.3",
"symfony/symfony": "2.1.*",
"zendframework/zendframework" : "2.1.3"
}
.....
3. Type :
composer update
4. Create a class to get Service From Your ZF2Module (SymfonyPrj/src/Acme\DemoBundle\Service\MyZFModuleService.php )
namespace Acme\DemoBundle\Service;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;
class MyZFModuleService
{
protected $sampleZFService;
public function __construct()
{
$serviceManager = new ServiceManager(new ServiceManagerConfig(array()));
$serviceManager->setService('ApplicationConfig', array(
'modules' => array(
'ZFModule'
),
'module_listener_options' => array()
));
$serviceManager->get('ModuleManager')->loadModules();
//MySampleService is a Service from Your ZF2 Module
$this->sampleZFService = $serviceManager->get('MySampleService');
}
public function dump()
{
echo '<pre>';
print_r($this->sampleZFService->getConfig());
echo '</pre>';
}
}
5. Register to Symfony Services :
...
<service id="MyZFModuleService" class="Acme\DemoBundle\Service\MyZFModuleService">
</service>
...
6. Ok, check it in your Symfony2 Controller Action :
$serviceZF = $this->get('MyZFModuleService');
$serviceZF->dump();
References :
1. http://zend-framework-community.634137.n4.nabble.com/Doctrine-2-module-is-tight-to-MVC-td4659346.html
[...] We can use a pieces of them in other framework if we want. I think, instead of write about using ZF2 Module in SF2 project, it worth to write about it ( SF2 Bundle in ZF2 Project [...]