doremi
doremi

Reputation: 15329

How to use 3rd-party libraries in Symfony 2

I have a class handles some API integration that I would like to be made available in my main site bundle that looks like so:

class ACH {
  public function __constructor();
  public function addACHTransaction();
  ...
}

How do I integrate this class within Symfony 2 so that I can use it within my controllers and entities by doing something like:

$ach = new ACH();

*Edit: * Here are the contents of my autoload.php file.

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__.'/../vendor/autoload.php';

// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

Upvotes: 1

Views: 5177

Answers (2)

Mun Mun Das
Mun Mun Das

Reputation: 15002

The ClassLoader Composer uses is different from the ClassLoader Symfony provides. Try following,

$achMap = array(
    'ACH' => 'path/of/ACH.php'
);
$loader->addClassMap($achMap);

Upvotes: 1

Juan Sosa
Juan Sosa

Reputation: 5280

Try the Symfony ClassLoader Component.

You may need to create a new wrapper Class in case your 3rd-party library doesn't follow symfony standards (i.e. use of namespaces). For sf2.0 take a look at this blog article.

EDIT: This reddit post explains how to deal with the Call to undefined method Composer\Autoload\ClassLoader::registerPrefixes() error. I haven't tested it but maybe the ClassLoader documentation is not up to date yet for sf2.1.

Add these lines in your app/autoload.php file:

$loader->add('foo_', __DIR__.'/../vendor/Foo/lib');
set_include_path(__DIR__.'/../vendor/Foo/lib'.PATH_SEPARATOR.get_include_path());

Upvotes: 3

Related Questions