Welcome to Abdul Malik Ikhsan's Blog

Symfony 2 : Using Symfony2 Bundle in Zend Framework 2 Project

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

symfony2-distributionsSymfony2 comes with Bundle, Zend Framework 2 comes with Module, The reusability code is coming. We can use a piece of them in other framework if we want. I think, besides of write about using ZF2 Module in SF2 project, it worth to write about it ( SF2 Bundle in ZF2 Project ).

Ok, let’s start, for example, I have an SF2 Bundle like the following :
Which has classes like the following :
a. ExampleDependentInterface

namespace San\MySFBundle\Classes;

interface ExampleDependentInterface
    public function run($parameter);

b. ExampleDependent class that implement ExampleDependentInterface

namespace San\MySFBundle\Classes;

class ExampleDependent implements ExampleDependentInterface
    public function run($environment)
        echo 'ExampleDependent run in '.$environment;

c. ExampleClass class that pass class implemented ExampleDependentInterface

namespace San\MySFBundle\Classes;

class ExampleClass
    protected $dependent;

    public function __construct(ExampleDependentInterface $dependent)
        $this->dependent = $dependent;

    public function run($env = 'Symfony2')

Now, register ExampleClass in services.php ( resources/config/services.php)

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

    'my_sample_service', new Definition('San\MySFBundle\Classes\ExampleDependent')

    new Definition(
            new Reference('my_sample_service')

Bundle ‘definitions’ done!, let’s copy to ZF2 Project like the following :

When Bundle copied, let’s configure Our ZF2 App.
1. create file named SF2AppKernel.php in ZF2 App ( config/SF2AppKernel.php )

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class SF2AppKernel extends Kernel
    public function registerBundles()
        $bundles = array(
            new San\MySFBundle\SanMySFBundle(),

        return $bundles;

    public function registerContainerConfiguration(LoaderInterface $loader){}

2. add require symfony/symfony to your composer.json

    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "zendframework/zftool": "dev-master",
        "symfony/symfony": "2.1.*"

4. Type

composer update

3. modify init_autoloader.php

if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
    $loader->add('San\\', __DIR__.'/module/SF2Bundles');

4. Modify public/index.php to initialize SF2 Bundles and SF2 Container, and register the Container into ZF2 ServiceManager

use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service;

define('REQUEST_MICROTIME', microtime(true));
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.

// Setup autoloading
require 'init_autoloader.php';

//load SF2 App Kernel here...
require 'config/SF2AppKernel.php';
$kernel = new SF2AppKernel('prod', true);

//initialize Bundles and Container

$SF2Container = $kernel->getContainer();

/* extract
    Zend\Mvc\Application::init(require 'config/application.config.php')->run();
    to the following to register SF2Container into ZF2 ServiceManager
$configuration = require 'config/application.config.php';
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
$serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
$serviceManager->setService('ApplicationConfig', $configuration);

$serviceManager->setService('SF2Container', $SF2Container);


5. Ok, configuration done!, let’s call the SF2 Bundle Service via Controller in Zend Framework 2 App :

$sl = $this->getServiceLocator()

References :
1. https://speakerdeck.com/skoop/zend-framework-2-and-symfony2-the-perfect-team-zendcon

13 Responses

Subscribe to comments with RSS.

  1. kelmadics said, on December 27, 2013 at 10:50 pm

    i am not very familiar with symfony but why cant you just use a simple ZF2 module and utilize init() or onBootstrap() to initialize the symfony kernel. is it a must to edit index.php?

    • samsonasik said, on December 27, 2013 at 11:24 pm

      no, it can be onBootstrap / init ofcourse🙂, but if you place on module, you’re tied to module, not the zf application itself. it based on your need.

      • kelmadics said, on December 27, 2013 at 11:39 pm

        ahh i see, thanks for the reply. for me personally though id rather have it everything inside a module to ease installation. it feels weird editing the index.php

      • samsonasik said, on December 29, 2013 at 2:59 am

        you’re welcome😉

  2. ojhaujjwal said, on January 7, 2014 at 10:14 am

    Wow, awesome post. I initially thought to re-write the code. Thanks for the tutorial

  3. seyferx said, on May 22, 2014 at 4:00 pm

    I can write zend2 module with minimum dependencies and make it composer packet.
    In packet set Zend dependencies.
    Than just use it in any project on any framework with psr0 autoload. And no need to install whole Zend framework, just some packets what module need.

    Bundle .. strange. Why all Symfony framework in requirements?)
    And what if bundle have own Controller, routing will work? I think not.

    This is not good idea use bundles this way. At first, modules need to be written for reuse and maximum independence from framework. Just library with psr0 and composer with some depends on framework libs.

    If module have own tpls and views, than it should run only on own framework.

    • samsonasik said, on May 22, 2014 at 6:01 pm

      mvc is outside of the re-usability component/lib/module/bundle as component .., I just take it as easiest way, I’m not dealing with symfony to deep

      • seyferx said, on May 22, 2014 at 6:08 pm

        But in your post ZF2 Module in SF2 project you include all Zend in require. Why not only module dependencies? Some list of Zend packages from packages.zendframework.com for module needs.

      • samsonasik said, on May 22, 2014 at 6:19 pm

        yes, this is just a sample for ‘interoperability’, you can try that if you think that better

  4. Gastón Cortés said, on June 16, 2015 at 12:38 am

    Hi Samsonasik

    Do you know if its possible to integrate a bundle in a ZF2 project but with fully functional mvc, like using its controllers and views?

    Thank you for this tutorial!

  5. Gastón Cortés said, on June 16, 2015 at 10:52 pm

    Thanks for your replay!

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: