【问题标题】:Is it possible to use Magento's existing / included Zend Framework in another app?是否可以在另一个应用程序中使用 Magento 现有/包含的 Zend 框架?
【发布时间】:2011-07-17 11:08:57
【问题描述】:

我想制作一个与我们的 Magento (Enterprise) 安装一起运行的 Zend 小应用程序。我可以使用 Magento 中包含的现有 Zend 代码吗?还是我需要另外一份 Zend 副本?

恐怕 Varien 可能把框架代码弄乱了。只是看起来他们已经注释掉了所有 require() 语句,这会引发很多错误(显然)。 Zend AutoLoader 无论如何都不能工作。有没有办法改用 Varien AutoLoader?

如果可以避免的话,我并不特别想将另一个框架(3000 多个文件)导入我们的项目。

谢谢!

编辑:

这是我的目录结构:

/localsite/ -- root
/localsite/products -- Magento install
/localsite/products/lib/Zend --Zend in Mage folder
/localsite/fbtest -- my Zend Framework app root
/localsite/fbtest/application -- my Zend Framework app

这是我正在尝试的代码(/localsite/fbtest/public/index.php):

<?php
define('DS', DIRECTORY_SEPARATOR);

defined('APPLICATION_PATH')
  || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

defined('APPLICATION_ENV')
  || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

set_include_path(implode(PATH_SEPARATOR, array(
  BASE_PATH . DS . 'products' . DS . 'lib' . DS . 'Zend',
  get_include_path(),
)));

require_once('../../products/lib/Zend/Application.php');

$application = new Zend_Application(
  APPLICATION_ENV,
  APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
        ->run();

这是错误:

Fatal error: Class 'Zend_Loader_Autoloader' not found in C:\xampp\htdocs\localsite\products\lib\Zend\Application.php on line 81

这是包含路径:

C:\xampp\htdocs\localsite\products\lib\Zend;.;C:\php\pear

这里是应该包含 AutoLoader 的地方(在 /products/lib/Zend/Application.php 中):

#require_once 'Zend/Loader/Autoloader.php';
$this->_autoloader = Zend_Loader_Autoloader::getInstance();

^^^ 看到 require_once 被注释掉的 '#' 了吗?我认为这是 Varien 所做的改变,它打破了框架,不是吗?这似乎是为什么它至少对我不起作用?我怎样才能解决这个问题并包含所有注释掉的包含??

再次感谢

【问题讨论】:

    标签: zend-framework magento


    【解决方案1】:

    以下代码已启用 Zend-Framework 在 cli 脚本中的使用。随 Magento 提供的 Zend-Framework 已用于 cli 脚本。

    // 1. point libPath to the <magento-root>/lib directory
    $libPath=realpath(dirname(__FILE__).'../../../lib') ;
    
    // 2. set the Zend-Framework include-path
    set_include_path($libPath . PATH_SEPARATOR . get_include_path());
    
    // 3. manual includes 
    
    require_once 'Zend/Loader.php';
    require_once 'Zend/Loader/Autoloader.php';
    
    // 4. instantiate the autoloader
    Zend_Loader_Autoloader::getInstance();
    

    这已启用 Zend_Http_Client 及其所有依赖类。

    【讨论】:

      【解决方案2】:

      好的,我知道了!带有 Magento 的 Zend 框架并没有真的损坏......但他们做的事情与正常情况略有不同。:

      1) 当您使用 Zend Loader 启用“延迟加载”时,注释掉 Zend 中的 require_once() 语句正常的。这是一个性能问题:
      http://framework.zend.com/manual/en/performance.classloading.html

      2) 在 Application.php 和 Loader.php 类中将它们注释掉是正常的,但是:

      在 Zend_Application 和 Zend_Loader_Autoloader 中[保留] require_once() 调用,因为没有它们这些类将失败。

      所以,答案是在启用 Loader.php 时自己添加 3 个缺少的 require_once() 语句。这是我的新代码(在我的应用程序 index.php 中):

      <?php
      define('DS', DIRECTORY_SEPARATOR);
      
      defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
      
      defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
      
      set_include_path(implode(PATH_SEPARATOR, array(
        BASE_PATH . DS . 'products' . DS . 'lib',
        '.',
      )));
      
      require_once 'Zend/Loader.php';
      
      require_once 'Zend/Loader/Autoloader.php';
      Zend_Loader_Autoloader::getInstance();
      
      require_once APPLICATION_PATH.'/Bootstrap.php';
      
      $application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
      );
      $application->bootstrap()
          ->run();
      

      现在我开始跑步了!问题是我真的不知道自动加载器是如何设置的。我希望这对某人有帮助!

      【讨论】:

        【解决方案3】:

        恐怕 Varien 可能把框架代码弄乱了。

        我猜这会违反使用条款(除了剥离 require_once)。

        Zend_Autoloader 应该可以正常工作 IMO。只需设置包含路径就可以了;)

        更新:

        我可以从您的代码中看到您没有定义 BASE_PATH 常量。因此未定义包含路径并且无法加载Zend_Loader_Autoloader。如果您的包含路径没问题,那么在没有路径的情况下编写 require_once 'Zend/Loader/Autoloader.php'; 就可以了。也没有必要要求 Zend_Loader 它已被弃用。

        您还包括Zend_Application 而不是Loader。为什么?只有需要“硬编码”的类是自动加载器,使用行require_once 'Zend/Loader/Autoloade.php'; - 仅此而已。如果它不起作用,则说明您的包含路径搞砸了。

        【讨论】:

        • 我在我的问题中包含了更多信息,只是设置包含路径不起作用。不过,关于 TOS 的有趣点……太糟糕了,但它并不能帮助我解决这个问题。 :)
        • 关于仍然需要Application.php的好点,我不再这样做了。
        【解决方案4】:

        添加 magento 的库文件夹以在 index.php 中包含路径:

        //define shortcut for DIRECTORY_SEPARATOR
        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
        //define base bath obtainable throughout the whole application
        defined('BASE_PATH')
            || define('BASE_PATH', realpath(dirname(__FILE__) . DS . '..' . DS . 'basedir' . DS));
        
        //define path to application directory
        defined('APPLICATION_PATH')
            || define('APPLICATION_PATH', BASE_PATH . DS . 'app');
        
        //set include path to libraries
        set_include_path(BASE_PATH . DS . 'library' . PATH_SEPARATOR . MAGENTO_DIR . DS . 'lib' . PATH_SEPARATOR . get_include_path());
        

        但是通过这种方法,您不能使用最新的 ZF 版本。所以单独安装是我的选择。

        自动加载器可以工作,这里简单的方法是将原始 Zend/Loader/ 放入您的应用程序库中,或者手动包含所有必需的自动加载器类(只有 3 个:加载器、自动加载器、Zend_Exception)

        这是我的完整项目名称/public/index.php:

        if (get_magic_quotes_gpc()) {
          function stripslashes_deep($value)
          {
            $value = is_array($value) ?
                  array_map('stripslashes_deep', $value) :
                  stripslashes($value);
        
            return $value;
          }
        
          $_POST = array_map('stripslashes_deep', $_POST);
          $_GET = array_map('stripslashes_deep', $_GET);
          $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
          $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
        
        //define shortcut for DIRECTORY_SEPARATOR
        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
        
        //define base bath obtainable throughout the whole application
        defined('BASE_PATH')
            || define('BASE_PATH', realpath(dirname(__FILE__) . DS . '..' . DS . 'basedir'));
        
        //define path to application directory
        defined('APPLICATION_PATH')
            || define('APPLICATION_PATH', BASE_PATH . DS . 'app');
        
        //set include path to libraries
        set_include_path(BASE_PATH . DS . 'library' . PATH_SEPARATOR . get_include_path());
        
        //bootstrap, and run application
        try {
            require_once 'Zend/Application.php';
            //create application and configure it.
            $application = new Zend_Application(
                getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production',
                array('config' => array(APPLICATION_PATH . DS . 'configs' . DS . 'application.ini'))
            );
            //run application
            $application->bootstrap()->run();
        } catch (Exception $e) {
         //here was logging logic
        }
        

        这就是您的 /localsite/fbtest/public/index.php 的外观:

        if (get_magic_quotes_gpc()) {
          function stripslashes_deep($value)
          {
            $value = is_array($value) ?
                  array_map('stripslashes_deep', $value) :
                  stripslashes($value);
        
            return $value;
          }
        
          $_POST = array_map('stripslashes_deep', $_POST);
          $_GET = array_map('stripslashes_deep', $_GET);
          $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
          $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
        
        //define shortcut for DIRECTORY_SEPARATOR
        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
        
        //define base bath obtainable throughout the whole application
        //keep your libraries and application out of public directory
        defined('BASE_PATH')
            || define('BASE_PATH', realpath(dirname(__FILE__) . DS . '..' . DS . 'basedir'));
        
        //define path to application directory
        defined('APPLICATION_PATH')
            || define('APPLICATION_PATH', BASE_PATH . DS . 'application');
        
        //if index.php in /localsite/fbtest/public/
        defined('MAGENTO_PATH')
            || define('MAGENTO_PATH', realpath(dirname(__FILE__) . DS . '..' . DS . '..' . DS . 'products'));
        
        //set include path to libraries
        //noticed magento library added? 
        set_include_path(BASE_PATH . DS . 'library' . PATH_SEPARATOR . MAGENTO_PATH . DS . 'lib' . PATH_SEPARATOR . get_include_path());
        
        //bootstrap, and run application
        try {
            require_once 'Zend/Application.php';
            require_once 'Zend/Loader.php';
            require_once 'Zend/Loader/Autoloader.php';
            require_once 'Zend/Exception.php';
            //create application and configure it.
            $application = new Zend_Application(
                getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production',
                array('config' => array(APPLICATION_PATH . DS . 'configs' . DS . 'application.ini'))
            );
            //run application
            $application->bootstrap()->run();
        } catch (Exception $e) {
         //here was logging logic
        }
        

        【讨论】:

        • 感谢您的回答!但它不起作用。如果您想再看一遍,我添加了更多信息。
        猜你喜欢
        • 1970-01-01
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        相关资源
        最近更新 更多