1

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;

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

5 Comments

I'm trying to follow the instructions, but I'm not sure where to call $loader->registerPrefixes() in my autoload.php file as $loader has already been defined. I added my autoload.php file above.
Adding it immediately after $loader = require DIR.'/../vendor/autoload.php'; should work.
It doesn't ... Fatal error: Call to undefined method Composer\Autoload\ClassLoader::registerPrefixes()
Sorry, last time I used ClassLoader it was on sf2.0 . Try adding it before $loader->register(); in /vendor/composer/autoload_real.php file instead.
I'm getting the same error. It can't find registerPrefixes().
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.