Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Zendframework 2 library with Symfony 2?

Tags:

symfony

How can I integrate zendframework 2 library with my Symfony 2 application? How to autoload and how to use it? I would like to use some classes.

like image 963
Marcos Avatar asked Feb 20 '23 21:02

Marcos


2 Answers

To integrate Zendframework 2 into Symfony 2, If you're using the Symfony Standard Distribution, add the following to the deps file at the root of your project:

[zf2]
    git=https://github.com/zendframework/zf2.git

Now, update the vendor libraries by running:

php bin/vendors update

If you are not using Symfony Standard Distribution, you will have to clone from github in vendor folder.

Next, add the Zend namespace to the app/autoload.php file so that these libraries can be autoloaded.

$loader->registerNamespaces(array(
    ...
    'Zend' => __DIR__ . '/../vendor/zf2/library',
));

Then It's done, You can use zendframework library. For example I will show Zend\Json class usage in the default symfony 2 application. Open src/Acme/DemoBundle/Controller/DemoController.php and edit indexAction method the code like below:

use Zend\Json\Json;
...
public function indexAction()
{
    $data = array('zendframework2' => 'symfony2');
    $encodedData = Json::encode($data);
    var_dump($encodedData);
    return array();
}

In this example I am using a zendframework class to convert an array to json

like image 181
dextervip Avatar answered Mar 12 '23 05:03

dextervip


For Symfony 2.1 you'll need to add zendframework/zendframework under require in composer.json. If you only want a few packages and not the whole library, you should add the url to the Zend libraries under repositories, like so:

"repositories": [
    {
        "type": "composer",
        "url": "http://packages.zendframework.com/"
    }
],
"require": {
    //...
    "zendframework/zend-log":"2.*",
like image 23
dan-klasson Avatar answered Mar 12 '23 05:03

dan-klasson