如何将第三方库添加到magento?

时间:2022-11-24 22:55:42

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

该库不需要与magento集成,它主要是与API通信的包装器。

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

我希望能够使用此库并在控制器或模型中进行这些API调用。

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

我在哪里可以放图书馆?如何将它们添加到自动装带器?

2 个解决方案

#1


46  

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

查看网站根目录中的/ lib文件夹。来自Magento基地目录:

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.

Magento的库文件夹是基于非模块的Magento代码所在的位置。这包括允许Magento运行的大量系统代码,以及许多第三方库(包括Zend Framework)。该库也是Magento在尝试自动加载文件时将搜索的最后一个代码池。

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

因此,换句话说,如果您的库支持zend文件命名约定 - 将由magento autoloader找到并加载库类。否则,您可以使用Mage :: getBaseDir('lib')获取/ lib目录的路径并编写类似的内容

require_once(Mage::getBaseDir('lib') . '/EZComponents/Base/src/base.php');

#2


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 :

作为一个完美运行的解决方案:您可以扩展varien_event_observer,创建自己的自动加载器功能,并通过使用controller_front_init_before事件将此自动加载器推送到__autoload堆栈前面。这个集成日光浴和交响乐事件调度员的例子可以解释它:

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 :)

当然你的图书馆应该在lib池中!这个解决方案由@Jeroen Vermeulen提供,我感谢他:)

#1


46  

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

查看网站根目录中的/ lib文件夹。来自Magento基地目录:

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.

Magento的库文件夹是基于非模块的Magento代码所在的位置。这包括允许Magento运行的大量系统代码,以及许多第三方库(包括Zend Framework)。该库也是Magento在尝试自动加载文件时将搜索的最后一个代码池。

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

因此,换句话说,如果您的库支持zend文件命名约定 - 将由magento autoloader找到并加载库类。否则,您可以使用Mage :: getBaseDir('lib')获取/ lib目录的路径并编写类似的内容

require_once(Mage::getBaseDir('lib') . '/EZComponents/Base/src/base.php');

#2


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 :

作为一个完美运行的解决方案:您可以扩展varien_event_observer,创建自己的自动加载器功能,并通过使用controller_front_init_before事件将此自动加载器推送到__autoload堆栈前面。这个集成日光浴和交响乐事件调度员的例子可以解释它:

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 :)

当然你的图书馆应该在lib池中!这个解决方案由@Jeroen Vermeulen提供,我感谢他:)

相关文章