简体   繁体   English

如何在我的项目中使用Zend Framework 2类?

[英]How to use Zend Framework 2 class in my project?

I juste want to have the possibility to use the class of this library but after 2 hours impossible : 我只是想有可能使用此库的类,但在2小时后无法使用:

So how to use the Zend Framework 2.1 with his autoloader to just use class of this library and not create a ZF project? 那么如何将Zend Framework 2.1与他的自动加载器一起使用以仅使用该库的类而不创建ZF项目?

I have try everything with the classmap_generator, inlude_path... i haven't any error but it still return me : 我已经尝试使用classmap_generator,inlude_path进行所有操作...我没有任何错误,但仍然返回我:

Could not find action class? 找不到动作课? Could not find version class! 找不到版本类别!

Thanks you. 谢谢。

You can install individual Zend Framework modules via composer, which will take care of most of this for you: 您可以通过composer安装单个Zend Framework模块,这将为您完成大部分工作:

composer.json composer.json

{
    "require": {
        "zendframework/zend-http": "2.*"
    },  
    "repositories": [
        {   
            "type": "composer",
            "url": "http://packages.zendframework.com/"
        }   
    ],  
    "minimum-stability": "dev"
}

this should take care of any autlloading for you. 这应该为您处理任何自动加载。 Composer would generate an autoloader which you would just include in your app: Composer会生成一个自动加载器,您只需将其包含在应用程序中即可:

require 'vendor/autoload.php';

there's a load of composer examples out there: 这里有很多作曲家的例子:

https://packages.zendframework.com/#composer https://packages.zendframework.com/#composer

http://net.tutsplus.com/tutorials/php/easy-package-management-with-composer/ http://net.tutsplus.com/tutorials/php/easy-package-management-with-composer/

Since ZF2 components are decoupled and focused on a specific task, one class often uses another to do its business. 由于ZF2组件是分离的并且专注于特定任务,因此一类经常使用另一类来开展业务。 Simply including a single class file is usually not sufficient. 仅包含一个类文件通常是不够的。

As a result, ZF2 definitely requires some autoloading. 结果,ZF2肯定需要一些自动加载。 But you don't have to use the ZF2 autoloader. 但是您不必使用ZF2自动加载器。

If you have no autoloading already in place in your app, then it's actually pretty easy to add: 如果您的应用程序中还没有自动加载功能,那么添加它实际上很容易:

  1. Make sure the path to the Zend library is on your include_path . 确保Zend库的路径在include_path
  2. Register a standard PSR-0-compliant autoloader using spl_autoload_register() 使用spl_autoload_register()注册符合PSR-0标准的自动加载器

Something like: 就像是:

spl_autoload_register(function($class)){
    $file = str_replace('\\', '/', $class) . '.php';
    $resolvedFile = stream_resolve_include_path($file);
    if (file_exists($resolvedFile)) {
        include $resolvedFile;
        return $class;
    }
});

If you already have an autoloader for your application, then I'd be quite surprised if it was not PSR-0-compliant. 如果您已经为您的应用程序安装了自动加载器,那么如果它不符合PSR-0标准,我会感到非常惊讶。 As before, make sure that the Zend library is on the include_path . 和以前一样,请确保Zend库位于include_path

Even if the Zend library is not on your include path, you can create a custom autoloader that is aware of where in your filesystem the library is based. 即使Zend库不在包含路径上,您也可以创建一个自定义的自动加载器,该加载器知道该库在文件系统中的位置。

In all cases, usage of the desired class is then: 在所有情况下,所需类的用法为:

$myinstance = new \Zend\Component\Class();

With the correct autoloading in place - however you choose to go about it - this class can use any other Zend classes that it needs to go about its business. 有了正确的自动加载功能后-您可以选择进行此操作-此类可以使用其开展业务所需的任何其他Zend类。

After many search, i have install composer. 经过多次搜索,我已经安装了作曲家。 But i'm not very happy of this solution cause it touch my system. 但是我对这个解决方案并不满意,因为它影响了我的系统。

Here is my procedure, i still say that ZF documentation is very poor. 这是我的程序,我仍然说ZF文档非常差。

Install ZEND FRAMEWORK WITH COMPOSER 使用COMPOSER安装ZEND FRAMEWORK

1) Allow whitelist 1)允许白名单

suhosin.executor.include.whitelist = phar In file : nano /etc/php5/cli/conf.d/suhosin.ini suhosin.executor.include.whitelist = phar在文件中:nano /etc/php5/cli/conf.d/suhosin.ini

2) Download of composer 2)下载作曲家

Apt-get install curl curl -sS https://getcomposer.org/installer | Apt-get install curl curl -sS https://getcomposer.org/installer | php 的PHP

3) Move composer 3)移动作曲家

mv composer.phar /usr/local/bin/composer mv composer.phar / usr / local / bin / composer

4) Copy the composer.json 4)复制composer.json

Copy composer.json (From zend framework archive) a my root folder application 复制composer.json(来自zend framework存档)到我的根文件夹应用程序

5) Composer install 5)Composer安装

Execute where the composer.json is 执行composer.json所在的位置

Now you will have a folder named "vendor" in your app folder, to use in your php code : 现在,您的应用程序文件夹中将有一个名为“ vendor”的文件夹,可用于您的php代码:

include("vendor/autoload.php");
new Zend\Mail\Storage\Pop3(...);

And now it work for me. 现在它对我有用。

Bug allowed memory size : 错误允许的内存大小:

php -r "echo ini_get('memory_limit').PHP_EOL;" php -r“ echo ini_get('memory_limit')。PHP_EOL;” change to 1024M nano /etc/php5/cli/php.ini /etc/init.d/apache2 restart 更改为1024M nano /etc/php5/cli/php.ini /etc/init.d/apache2重新启动

Failed to clone http://github.com/zendframework/Component_ZendStdlib.git , git was not found, 无法克隆http://github.com/zendframework/Component_ZendStdlib.git ,找不到git,

Install GIT Apt-get install git 安装GIT Apt-get install git

Just register the namespace in your autoloader. 只需在自动加载器中注册名称空间即可。

if you are using composer then add include_once '<path to vendor dir>/autoload.php'; 如果您使用的是作曲家,则添加include_once '<path to vendor dir>/autoload.php'; on top of your php file 在您的php文件之上

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

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