如何在Zend Framework 2中包含第三方lib [英] How to include third party lib in Zend Framework 2

查看:31
本文介绍了如何在Zend Framework 2中包含第三方lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从ZF1迁移到ZF2.我有一个控制器依赖于第三方库'Solarium'.

I'm migrating application from ZF1 to ZF2. I have a controller depends on third party lib 'Solarium'.

namespace Stock\Controller;
class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        require_once('Solarium/Autoloader.php');
        Solarium_Autoloader::register();

太阳能"存在于供应商"下,在"init_autoloader.php"中,我有:

The 'Solarium' exists under 'vendor', and in 'init_autoloader.php' I have:

set_include_path(implode(PATH_SEPARATOR, array(
    realpath('vendor')
)));

但是,当我查看页面时,出现错误:

But, when I viewing the page, there's an error:

Fatal error: Class 'Stock\Controller\Solarium_Autoloader' not found in ...


我试图在'StandardAutoloader.php'中添加跟踪,发现在运行时调用了StandardAutoloader.autoload('Stock \ Controller \ Solarium_Autoloader').


I tried to add trace in 'StandardAutoloader.php' and found StandardAutoloader.autoload('Stock\Controller\Solarium_Autoloader') is called on runtime.

我想知道这是怎么发生的以及如何解决.谢谢.

I want to know how this happens and how to fix it. Thanks.

推荐答案

正如Aydin Hassan在其评论中所写,最简单的方法是使用Composer.首先,在项目的根目录下编辑composer.json文件,如下所示:

As Aydin Hassan wrote in his comment, the easiest way to make this work is by using Composer. First, edit your composer.json file within your project's root directory to look something like this:

"require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.*",
    "solarium/solarium": ">=2.4.0"
}

如果您正在使用 Zend Skeleton应用程序,那么在项目的根目录中还将拥有Composer本身目录(composer.phar).在这种情况下,您可以这样:

If you are using the Zend Skeleton Application, then you will also have Composer itself in your project's root directory (composer.phar). In that case, you can do like this:

cd /path/to/project && php composer.phar install solarium/solarium

cd /path/to/project && php composer.phar install

否则,只需在命令行的任何位置提供Composer.通过执行上述操作,Composer将为您完成自动加载.然后,在控制器内,您不必担心会包含文件,因为spl_autoload_register会自动为您提供文件.您只需要使用名称空间.您可以使用以下两种方法之一:

Otherwise just have Composer available everywhere in your command line. By doing like above, Composer will take care of autoloading for you. Within your controller, you should then not worry about including the file as this happens automatically for you with spl_autoload_register. You just have to use the namespace. You can use either of these two approaches:

namespace Stock\Controller;

use Solarium\Autoloader;

class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        Autoloader::register();
    }
}

namespace Stock\Controller;

class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        \Solarium\Autoloader::register();
    }
}

这篇关于如何在Zend Framework 2中包含第三方lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆