php 如何将第 3 方库添加到 magento?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6417394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:19:48  来源:igfitidea点击:

how to add a 3rd party library to magento?

phpmagentozend-frameworkmagento-1.7zend-autoloader

提问by john

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?

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

回答by Dmytro Zavalkin

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 框架)。该库也是 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 自动加载器找到并加载。否则,您可以使用 Mage::getBaseDir('lib') 获取 /lib 目录的路径并编写类似

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

回答by Mohamed23gharbi

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

并且您的库肯定应该在库中!此解决方案由@Jeroen Vermeulen 提供,我感谢他:)