How to use Zend Framework 2 class in my project?

2019-09-14 02:21发布

问题:

I juste want to have the possibility to use the class of this library but after 2 hours impossible :

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?

I have try everything with the classmap_generator, inlude_path... i haven't any error but it still return me :

Could not find action class? Could not find version class!

Thanks you.

回答1:

You can install individual Zend Framework modules via composer, which will take care of most of this for you:

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:

require 'vendor/autoload.php';

there's a load of composer examples out there:

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

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



回答2:

Since ZF2 components are decoupled and focused on a specific task, one class often uses another to do its business. Simply including a single class file is usually not sufficient.

As a result, ZF2 definitely requires some autoloading. But you don't have to use the ZF2 autoloader.

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.
  2. Register a standard PSR-0-compliant autoloader using spl_autoload_register()

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. As before, make sure that the Zend library is on the 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.

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.



回答3:

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.

Install ZEND FRAMEWORK WITH COMPOSER

1) Allow whitelist

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

2) Download of composer

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

3) Move composer

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

4) Copy the composer.json

Copy composer.json (From zend framework archive) a my root folder application

5) Composer install

Execute where the composer.json is

Now you will have a folder named "vendor" in your app folder, to use in your php code :

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;" change to 1024M nano /etc/php5/cli/php.ini /etc/init.d/apache2 restart

Failed to clone http://github.com/zendframework/Component_ZendStdlib.git, git was not found,

Install GIT Apt-get install git



回答4:

Just register the namespace in your autoloader.

if you are using composer then add include_once '<path to vendor dir>/autoload.php'; on top of your php file