天天看點

Zend Studio13.5建立Zend Framework 2.4.9時遇到的問題以及解決方案

在用Zend Studio13.5建立Zend Framework 2.4.9時遇到的問題以及解決方案:

出現的錯誤:

Fatal error: Uncaught exception ‘RuntimeException’ with message ‘Unable to load ZF2. Run

php composer.phar install

or define a ZF2_PATH environment variable.’ in D:\wampserver\wamp\www\Zend\init_autoloader.php on line 54

RuntimeException: Unable to load ZF2. Run

php composer.phar install

or define a ZF2_PATH environment variable. in D:\wampserver\wamp\www\Zend\init_autoloader.php on line 54

Zend Studio13.5建立Zend Framework 2.4.9時遇到的問題以及解決方案

意思就是找不到ZF2這個架構,看一下

init_autoloader.php

檔案,沒有興趣看原理的可以直接看後面的解決方案

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

/**
 * This autoloading setup is really more complicated than it needs to be for most
 * applications. The added complexity is simply to reduce the time it takes for
 * new developers to be productive with a fresh skeleton. It allows autoloading
 * to be correctly configured, regardless of the installation method and keeps
 * the use of composer completely optional. This setup should work fine for
 * most users, however, feel free to configure autoloading however you'd like.
 */

// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
}

if (class_exists('Zend\Loader\AutoloaderFactory')) {
    return;
}

$zf2Path = false;

if (is_dir('vendor/ZF2/library')) {
    $zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) {      // Support for ZF2_PATH environment variable or git submodule
    $zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
    $zf2Path = get_cfg_var('zf2_path');
}

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
        $loader->add('ZendXml', $zf2Path);
        // include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}

if (!class_exists('Zend\Loader\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}
           

可以從這裡下載下傳Zend Framework包,看一下下面的解壓目錄,

Zend Studio13.5建立Zend Framework 2.4.9時遇到的問題以及解決方案

其實主要加載的是

Zend

ZendXml

這兩個目錄,簡單說一下這個自動加載檔案是怎樣找這兩個目錄的

1、先嘗試用Composer這個管理工具加載

具體檔案在

vendor/autoload.php

vendor/composer/autoload_real.php

這兩個檔案中,前一個檔案調用後一個檔案執行庫查找,後一個檔案在

vendor/composer/autoload_namespaces.php

vendor/composer/autoload_psr4.php

vendor/composer/autoload_classmap.php

查找需要的配置檔案,而後兩個檔案中放的就是

Zend

ZendXml

的工作路徑

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Zend\\' => array($vendorDir . '/zendframework/zendframework/library/Zend'),
);

           
<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'ZendXml' => array($vendorDir . '/zendframework/zendxml/library'),
);

           

然後你會發現這兩個路徑下啥東西也沒有,是以你可以建立

Zend

ZendXml

兩個路徑,把相應的包導進去,一定要在

Zend Studio

就是在

/zendframework/zendxml/library

上右鍵 -> 建立(兩個)檔案夾 -> 導入(import),如果你在項目目錄下建立,再怎麼重新整理也找不到建立的目錄,感覺很不爽啊

2、用

getenv('ZF2_PATH')

加載

這個是要在

Apache

http.conf

檔案夾配置的。比如說我的

wamp

的配置檔案目錄就是

D:\wampserver\wamp\bin\apache\apache2.4.18\conf\http.conf

,在後面加上下面這行代碼,後面的目錄替換成你自己的

library

,重新開機服務

SetEnv ZF2_PATH "E:\Zend\library"
           

3、用

get_cfg_var('zf2_path')

加載

這個配置檔案是

php.ini

,我的目錄是

D:\wampserver\wamp\bin\apache\apache2.4.18\bin\php.ini

,加上下面一句,重新開機服務

zf2_path = "E:\Zend\library"
           

對于後面兩個,還要改一下

init_autoloader.php

檔案:

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
        $loader->add('ZendXml', $zf2Path);
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; // add this
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}
           

即使你後來删除了環境變量還是會存在,如果不想用了,就設定成空,或者到php 手冊中尋找方案。

解決方案

1、在

/zendframework/zendxml/library

下右鍵建立兩個目錄:

Zend

ZendXml

後,再把下載下傳好的庫檔案

ZendFramework-2.4.9\library

導入,最後

/zendframework/zendxml/library

這個目錄下大概是這個樣子:

Zend Studio13.5建立Zend Framework 2.4.9時遇到的問題以及解決方案

2、在

Apache

http.conf

檔案(參考路徑:

D:\wampserver\wamp\bin\apache\apache2.4.18\conf\http.conf

)中添加下面這行代碼

SetEnv ZF2_PATH "E:\Zend\library"
           

重新開機服務,修改

init_autoloader.php

檔案

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
        $loader->add('ZendXml', $zf2Path);
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; // add this
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}
           

3、在

php

php.ini

檔案(參考路徑:

D:\wampserver\wamp\bin\apache\apache2.4.18\bin\php.ini

)中添加下面這行代碼

zf2_path = "E:\Zend\library"
           

重新開機服務,修改

init_autoloader.php

檔案

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
        $loader->add('ZendXml', $zf2Path);
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; // add this
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}