21

The library doesn't need to integrate with magento, it's mostly a wrapper that communicates with an API.

I would like to be able to use this library and make these API calls from within a controller or model.

Where can I put the library? How do I add them to the autoloader?

Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
john
  • 33,520
  • 12
  • 45
  • 62

2 Answers2

49

Look into /lib folder in your website root directory. From Magento Base Directories:

Magento’s library folder is where non-module based Magento code lives. This include a large amount of the system code which allows Magento to run, as well as a number of third party libraries (including the Zend Framework). The library is also the last code pool Magento will search when attempting to autoload a file.

So, in other words, if your library supports zend file naming convention - library classes will be found and loaded by magento autoloader. Otherwise you can get path of your /lib directory with Mage::getBaseDir(‘lib’) and write something like

require_once(Mage::getBaseDir('lib') . '/EZComponents/Base/src/base.php');
B00MER
  • 5,471
  • 1
  • 24
  • 41
Dmytro Zavalkin
  • 5,265
  • 1
  • 30
  • 34
  • One of the good approach to autoload library - http://stackoverflow.com/questions/4085967/how-to-integrate-ezcomponents-with-magento/4636662#4636662 – Kamal Joshi May 21 '14 at 09:59
9

As a solution that works perfectly: you can extend the varien_event_observer, create your own autoloader function and and by using the controller_front_init_before event you push this autoloader in front of the __autoload stack. this example of integrating solarium and symphony event dispatcher can explain it :

class JeroenVermeulen_Solarium_Model_Observer_Autoloader extends Varien_Event_Observer {

    /**
     * This an observer function for the event 'controller_front_init_before'.
     * It prepends our autoloader, so we can load the extra libraries.
     *
     * @param Varien_Event_Observer $event
     */
    public function controllerFrontInitBefore( $event ) {
        spl_autoload_register( array($this, 'load'), true, true );
    }

    /**
     * This function can autoloads classes starting with:
     * - Solarium
     * - Symfony\Component\EventDispatcher
     *
     * @param string $class
     */
    public static function load( $class )
    {
        if ( preg_match( '#^(Solarium|Symfony\\\\Component\\\\EventDispatcher)\b#', $class ) ) {
            $phpFile = Mage::getBaseDir('lib') . '/' . str_replace( '\\', '/', $class ) . '.php';
            require_once( $phpFile );
        }
    }

}

and surely your libraries shoud be in the lib pool ! this solution is provided by @Jeroen Vermeulen, and i thank him :)

Community
  • 1
  • 1
Mohamed23gharbi
  • 1,710
  • 23
  • 28
  • 2
    It works perfectly !! Thanks ! Used this for using Bpost library (https://github.com/tijsverkoyen/bpost) with Magento. Class exemple: https://github.com/tijsverkoyen/bpost/blob/master/Bpost/Order/Box/AtHome.php – Jona May 19 '14 at 18:49
  • Woot keep the advertising for our extension comming :p – Toon Van Dooren Jun 10 '15 at 13:47
  • @JonaPkr can you update your link with the example (https://github.com/tijsverkoyen/bpost/blob/master/src/Bpost/Order/Box/AtHome.php) ? :) – Ricardo Martins Aug 27 '18 at 04:37
  • @RicardoMartins sorry i don't know why but i can't edit my comment :s Guys, my first link is outdated, please follow the good one, tx to Ricardo https://github.com/tijsverkoyen/bpost/blob/master/src/Bpost/Order/Box/AtHome.php – Jona Sep 15 '18 at 11:23