是否可以在另一个应用程序中使用 Magento 现有的/包含的 Zend 框架?

发布于 2024-10-23 14:15:23 字数 1877 浏览 4 评论 0原文

我想制作一个 Zend 小应用程序,与我们的 Magento(企业)安装一起运行。我可以使用 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

这是 include_path:

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 所做的改变打破了框架,不是吗?至少这似乎就是为什么它对我不起作用?我怎样才能解决这个问题并包含所有注释掉的内容?

再次感谢

I want to make a little Zend app that runs along side our Magento (Enterprise) install. Can I use the existing Zend code included with Magento? Or do I need to have another separate copy of Zend?

I am afraid that Varien has probably messed with the framework code. Just looking it appears they have commented out all require() statements, which is throwing lots of errors (obviously). The Zend AutoLoader won't work, at any rate. Is there a way to use the Varien AutoLoader instead?

I don't particularly want to import another framework (3000+ files) into our project if I can avoid it.

Thanks!

EDIT:

Here is my dir structure:

/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

Here is the code I am trying (/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();

Here is the error:

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

Here is the include_path:

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

And here is where AutoLoader should be included (in /products/lib/Zend/Application.php):

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

^^^ see that '#' where the require_once is commented out? I think that's a change Varien made which breaks the Framework, no? It appears to be why it's not working form me, at least? How can I get around this and include all of the commented out includes??

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-10-30 14:15:23

添加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:loader、autoloader、Zend_Exception)

这是我的完整的projectName/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
}

add magento's library folder to include path in 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());

But with this approach you can't use latest ZF version. So separate install is my choice.

autoloader works, easy way here is to place original Zend/Loader/ into your application library, or include all required autoloader classes manually(just 3: loader, autoloader, Zend_Exception)

Here is my full projectName/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
}

This is how your /localsite/fbtest/public/index.php may look:

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
}
十年九夏 2024-10-30 14:15:23

好吧,我明白了! Zend 框架与 Magento 并没有真正被破坏...但是他们做的事情与正常情况有点不同。:

1) 注释掉 require_once(正常的) ) 当您使用 Zend Loader 启用“延迟加载”时,Zend 中的语句。这是一个性能问题:
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();

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

Alright, I got it! The Zend Framework with Magento isn't really broken... but they are doing things a little different than normal.:

1) It is normal to comment out the require_once() statements in Zend when you enable "lazy loading" with the Zend Loader. It's a performance thing:
http://framework.zend.com/manual/en/performance.classloading.html

2) It is not normal to comment them out in the Application.php and Loader.php classes, however:

[keep the] require_once() calls within Zend_Application and Zend_Loader_Autoloader, as these classes will fail without them.

So, the answer is to just add the 3 missing require_once() statements yourself when enabling Loader.php. Here is my new code that works (in my apps 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();

Now I am off and running! The problem was that I just didn't really know how the auto-loader was set up. I hope this helps someone!

深居我梦 2024-10-30 14:15:23

我担心 Varien 可能搞乱了框架代码。

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

在我看来,Zend_Autoloader 应该可以正常工作。只需设置包含路径就可以了;)

更新:

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

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

I am afraid that Varien has probably messed with the framework code.

I guess that would violate the terms of use (except stripping the require_once).

Zend_Autoloader should work fine IMO. Just setup the include path and you're ok ;)

Update:

I can see from your code that you have not the BASE_PATH constant defined. Therefore the include path is not defined and Zend_Loader_Autoloadercan't be loaded. If your include path would be OK, then it's fine to write require_once 'Zend/Loader/Autoloader.php'; without the path. Also there is no need to require Zend_Loader it's deprecated.

Also you're including Zend_Application not Loader. Why? Only class ever needed to be 'hardcode' required is the autoloader, using the line require_once 'Zend/Loader/Autoloade.php'; - NOTHING MORE. If it doesn't work, your include path is messed up.

饮惑 2024-10-30 14:15:23

以下代码启用了在 cli 脚本中使用 Zend-Framework。与 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 及其所有依赖类。

The following code has enabled the usage of the Zend-Framework in a cli script. The Zend-Framework, that is delivered with Magento, has been used for the cli script.

// 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();

This has enabled to use Zend_Http_Client and all its dependend classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文