0

I have stored a third party php library in /vendor/library folder. Now i need to import it to my Zend app and use it inside controller action.

require_once ('/vendor/library/client.php');

Is this correct ? Or there is other way to to handle this ?

ChamingaD
  • 2,908
  • 8
  • 35
  • 58
  • possible duplicate of [How to include third party lib in Zend Framework 2](http://stackoverflow.com/questions/16000465/how-to-include-third-party-lib-in-zend-framework-2)... basically: edit your composer.json file and run `php composer.phar install` again, to regen your autoloader and paths – Elias Van Ootegem Sep 06 '13 at 07:02
  • @EliasVanOotegem I can include any php files with composer like that ? Library that i use is not composer ready. – ChamingaD Sep 06 '13 at 08:01
  • in the composer.json file, there's an `autoload` property. tp wjocj ypi cam add `psr-0` compliant libs, too... google how – Elias Van Ootegem Sep 06 '13 at 08:22
  • 1
    Which version of ZF are we talking about here? When you say library, do you mean that single file client.php? – Michal M. Sep 06 '13 at 08:28
  • I updated composer.json and run it. I got Problem 1 - The requested package salesforce/library could not be found in any version , there may be a typo in the package name. – ChamingaD Sep 06 '13 at 08:30
  • @M.M. ZF2. Library has many files but as the documentation i only need to import one file - require_once ('soapclient/SforceEnterpriseClient.php'); http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_Toolkit_for_PHP – ChamingaD Sep 06 '13 at 08:41

2 Answers2

2

Use the ZF autoloader, then forget about include/require.

http://framework.zend.com/manual/1.12/en/zend.loader.autoloader.html

It means though that your class names and file names have to follow their naming conventions - which may be more trouble than it is worth.

But if you are developing your own library to work within ZF, then it is a good idea.

Cups
  • 6,901
  • 3
  • 26
  • 30
  • Note the path: `vendor` indicates composer is being used, and the tag zf2 indicates that a link to zf1.12 docs is not helpful – Elias Van Ootegem Sep 06 '13 at 07:01
  • yay, zf2 tag added later I see. Oh well. So, vendor=composer, thx @Elias Van Ootegem – Cups Sep 06 '13 at 07:28
  • @Cups I can't rewrite that library. Its as Soap service which i downloaded. How can i include it without doing any changes ? – ChamingaD Sep 06 '13 at 07:55
  • @EliasVanOotegem So, where i should keep my external libraries in Zend ? – ChamingaD Sep 06 '13 at 08:00
  • the vendor dir does not mean composer, anybody can name their vendor dir 'vendor', as many people do... since they put code their from different... vendors – NDM Sep 06 '13 at 08:53
2

Adding a Composer ready 3rd party library to a ZF2 instance

The correct way to add a 3rd party library is to use Composer.

E. g. if you wish to add ZfcUser to your Zend Framework 2 application use the following command:

composer require zf-commons/zfc-user:dev-master

This will download the code from github and you just need to add the module name to your: /config/application.config.php.

Adding other 3rd party library to a ZF2 instance

If your 3rd party library is not Composer ready, you can add it to your Zend Framework 2 instance by creating a Module for it.

Step 1

/vendor/MyModule/Module.php

<?php
namespace MyModule;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

What this basically provides is a way for you to register your 3rd party code within a file called autoload_classmap.php:

Step 2

/vendor/MyModule/autoload_classmap.php

<?php
return array(
    'MyModule\VendorLibrary' => __DIR__ . '/src/MyModule/VendorLibrary.php',
);

Step 3

Your 3rd party code should reside in:

/vendor/MyModule/src/MyModule/VendorLibrary.php and could read something like this:

<?php
namespace MyModule;

class VendorLibrary
{
    public function sayHi($name)
    {
        return "Hi there, $name!";
    }
    // your 3rd party code ...
}

Step 4

Add your new module to application.config.php:

/config/application.config.php

<?php
return array(
    'modules' => array(
        // your other modules here ...
        'MyModule'
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

Usage

In your Controller you now use your vendor class like:

$vendor = new \MyModule\VendorLibrary();
$hi = $vendor->sayHi('John');

While it is a lot easier to use require_once(), it is not advisable because:

  1. it does not provide predictability and structure of your class hierarchy and location
  2. you also need to take care of include paths and make sure require_once is present in all controllers that need the 3rd party features
  3. it does not allow for overriding classes (Magento-style) etc.

Hope this helps!

mihaidoru
  • 327
  • 2
  • 10