Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Using Zend Framework 2 Module in Symfony 2 Project

Posted in Symfony2 Framework, Tutorial PHP, Zend Framework 2 by samsonasik on March 1, 2013

zf2-zendframework2In 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;

        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(
               'module_listener_options' => array()
        //MySampleService is a Service from Your ZF2 Module 
        $this->sampleZFService = $serviceManager->get('MySampleService');
    public function dump()
        echo '<pre>';
        echo '</pre>';

5. Register to Symfony Services :

       <service id="MyZFModuleService" class="Acme\DemoBundle\Service\MyZFModuleService">

6. Ok, check it in your Symfony2 Controller Action :

$serviceZF = $this->get('MyZFModuleService');

References :
1. http://zend-framework-community.634137.n4.nabble.com/Doctrine-2-module-is-tight-to-MVC-td4659346.html

3 Responses

Subscribe to comments with RSS.

  1. […] 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 […]

  2. seyferx said, on May 22, 2014 at 6:09 pm

    So. Why not include only module dependencies? Not whole Zend framework.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: