简体   繁体   English

如何仅加载Zend Framework 2数据库模块

[英]How do I load Zend Framework 2 database module only

Zend Framework 2 claims to have a "use-at-will" design that allows you to easily use any of its modules without committing to the full stack. Zend Framework 2声称具有“随意使用”设计,使您可以轻松使用其任何模块而无需提交完整的堆栈。 I need a good database access layer, and from the docs and recommendations online I like the look of Zend\\Db. 我需要一个良好的数据库访问层,并且从在线文档和建议中我喜欢Zend \\ Db的外观。 I've placed the Zend/Db folder in my /lib directory, but I'm having trouble getting PHP to recognize the Zend\\Db\\Adapter\\Adapter class. 我已经将Zend / Db文件夹放在我的/ lib目录中,但是我很难让PHP识别Zend \\ Db \\ Adapter \\ Adapter类。 I keep getting a fatal error when I try to use it: 尝试使用它时,我总是遇到致命错误:

Fatal error: Class 'Zend\\Db\\Adapter\\Adapter' not found in /home/username/public_html/test.php on line 6 致命错误:在第6行的/home/username/public_html/test.php中找不到类'Zend \\ Db \\ Adapter \\ Adapter'

I've tried setting ZF2_PATH in my .htaccess: SetEnv ZF2_PATH /home/username/public_html/lib/Zend 我尝试在.htaccess中设置ZF2_PATH: SetEnv ZF2_PATH /home/username/public_html/lib/Zend

I've tried setting the include path in my code: set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/lib' . PATH_SEPARATOR . get_include_path() ); 我尝试在代码中设置包含路径: set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/lib' . PATH_SEPARATOR . get_include_path() );

I've tried explicitly loading and instantiating the Zend\\Loader: 我试过显式加载和实例化Zend \\ Loader:

require_once 'lib/Zend/Loader/StandardAutoloader.php';
$zendLoader = new Zend\Loader\StandardAutoloader();
$zendLoader->register();

None of these have had any effect. 这些都没有任何作用。 I tried explicitly requiring Zend/Db/Adapter/Adapter.php, and that did fix the error I'm seeing, but then I just get the same error for one of its dependencies, so that's not a practical solution. 我尝试明确要求Zend / Db / Adapter / Adapter.php,并且确实解决了我所看到的错误,但是随后我因其依赖项之一而得到了相同的错误,因此这不是一个实际的解决方案。

What am I doing wrong here? 我在这里做错了什么? Is ZF2 just not really designed for this kind of modular usage, or am I missing something? ZF2真的不是真的为这种模块化用途而设计的,还是我缺少什么?

EDIT: I got this to work by writing my own autoload function: 编辑:我通过编写自己的自动加载功能使它起作用:

function autoloader($class) {
    $path = explode('\\', $class);
    foreach ($path as $p) {
        $cp .= DIRECTORY_SEPARATOR . $p;
    }
    include __DIR__ . '/lib/' . $cp . '.php';
}
spl_autoload_register(autoloader);

This sort of makes sense -- obviously if I'm using the db module without the rest of the framework, I can't expect the framework to do autoloading for me -- except I still don't understand why manually loading Zend\\Loader didn't solve the problem. 这种方式很有意义-显然,如果我在不使用框架其余部分的情况下使用db模块,则我不能指望框架会为我自动加载-除非我仍然不明白为什么手动加载Zend \\ Loader没有解决问题。 Isn't handling autoloading the point of Zend\\Loader? 不是在自动加载Zend \\ Loader的点吗? Anyway, I have a workable solution for now, but if there's a better solution I'd love to hear it. 无论如何,我现在有一个可行的解决方案,但是如果有更好的解决方案,我很想听听。

I strongly recommend you check out composer . 我强烈建议您检查作曲家 It really simplifies managing dependencies between a ton of modern PHP libraries, as well as autoloading. 它确实简化了管理大量现代PHP库之间的依赖关系以及自动加载的过程。

For instance, if you were starting you project, and you knew you wanted to pull in zend-db, you'd do something like this: 例如,如果您正在启动项目,并且知道要引入zend-db,则可以执行以下操作:

$ mkdir myproject
$ cd myproject
$ curl -s https://getcomposer.org/installer | php   
$ ./composer.phar require zendframework/zend-db:2.1.1

That last line will cause composer to spring into action. 最后一行将使作曲家开始行动。 It will create a directory called "vendor" where it keeps all the libraries it manages. 它将创建一个名为“ vendor”的目录,该目录将保留其管理的所有库。 It then will check out version 2.1.1 of zend-db in there, and set up an vendor/autoload.php which you'll require in your project. 然后它将在其中签出zend-db的2.1.1版本,并设置您在项目中需要的vendor / autoload.php。

Then you can test it out. 然后,您可以对其进行测试。 Create myproject/index.php like so: 像这样创建myproject / index.php:

<?php
require_once "vendor/autoload.php";

$adapter = new Zend\Db\Adapter\Adapter(array(
   'driver' => 'Pdo_Sqlite',
   'database' => 'path/to/sqlite.db'
));

And it just works. 而且就可以了。

Later, if you decide you need Zend\\Mail too, you just: 以后,如果您决定也需要Zend \\ Mail,则只需:

$ ./composer.phar require zendframework/zend-mail:2.1.1

and composer will install it, along with a couple of dependencies, and make sure it's available to the autoloader. 并且composer将安装它,以及几个依赖项,并确保它对自动加载器可用。

Composer is not ZF-specific, either. Composer也不是ZF特定的。 There's a whole ecosystem of code to explore. 有一个完整的代码生态系统可供探索。 Packagist is a good place to start. Packagist是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM