Zend框架模型包含路径配置完美,但加载失败


zend framework model inclusion path configured perfectly and failed to load

        Warning: include_once(Application/Model/Hiring.php): failed to open stream: 
No such file or directory in /var/www/hiring/library/Zend/Loader.php on line 146

包含路径

Warning: include_once(): Failed opening 'Application/Model/Hiring.php' for inclusion 
(include_path='/var/www/hiring/application/../library:/var/www/hiring/library:./application
/models/:./application/controllers/:./application/views/scripts/:.:/usr/share/php:/usr/local
/ZendFramework/library') in /var/www/hiring/library/Zend/Loader.php on line 146

,我的索引文件是

<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// define root path
defined('ROOT_PATH') || define('ROOT_PATH', realpath(dirname(__FILE__) . '/'));
// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(realpath(dirname(__FILE__) . '/library')
. PATH_SEPARATOR . './application/models/'
. PATH_SEPARATOR . './application/controllers/'
. PATH_SEPARATOR . './application/views/scripts/'
. PATH_SEPARATOR . get_include_path());
require_once 'Zend/Application.php';
require_once 'Zend/Loader/Autoloader.php';
/** Zend_Application */

$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('hiring');
$loader->setFallbackAutoloader(true);
Zend_Loader::loadClass('Zend_Controller_Front');
Zend_Loader::loadClass('Zend_Config_Ini');
Zend_Loader::loadClass('Zend_Registry');
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Db_Table');
Zend_Loader::loadClass('Zend_Db_Statement');
//Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
//Zend_Loader::loadClass('Zend_Mail_Transport_Sendmail');
//Zend_Loader::loadClass('Zend_Mail');
Zend_Loader::loadClass('Zend_Session_Namespace');
Zend_Loader::loadClass('Zend_Db_Adapter_Pdo_Pgsql');
//Zend_Loader::loadClass('Zend_Date');
Zend_Loader::loadClass('Zend_Log');

// setup controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(false);
$frontController->setBaseUrl('http://hiring.local');
$frontController->setControllerDirectory('/application/controllers');
// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap()->run();

为什么我得到这个错误我看了loader。php在第146行在那行有

include_once($filename)所以错误是从那里开始的

这里有相当多的问题:

  • application/modelsapplication/controllersapplication/views/scripts不应该在包含路径上。
  • $loader->registerNamespace('hiring');可能应该是$loader->registerNamespace('Hiring_');(尽管在您包含的代码示例中没有迹象表明您正在使用这个名称空间)。
  • $loader->setFallbackAutoloader(true);可能不需要(在您包含的代码示例中没有迹象表明您需要它)。
  • 应删除所有Zend_Loader::loadClass行。自动加载器的全部意义就在于你不需要自己加载类
  • 至少前端控制器配置应该移动到你的引导类

但是这些都不会影响你报告的问题。标准的自动加载器设置将只加载那些名称可以直接映射到文件系统的类(通过将路径中的下划线转换为斜杠,例如类Zend_Db_Table将存在于library/Zend/Db/Table.php中)。类Application_Model_Hiring不适合这个模型,如果你想使用这个命名方案,你还需要设置一个资源自动加载器,它将类名的最后一部分映射到application/中一些预定义的子文件夹。

将以下方法添加到引导类中:

protected function _initAutoloader()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('Hiring_');
    $applicationResourceAutoloader = new Zend_Loader_Autoloader_Resource(array(
        'basePath' => APPLICATION_PATH,
        'namespace' => 'Application'
    ));
    $autoloader->pushAutoloader($applicationResourceAutoloader);
    return $autoloader;
}

这将设置标准的自动加载器($autoloader = Zend_Loader_Autoloader::getInstance();),它将自动加载Zend Framework类。然后它注册一个命名空间'Hiring',只有当你在库文件夹中包含以该名称开头的类时才需要它。

然后它创建一个单独的资源自动加载器,命名空间为"Application",它将从应用程序文件夹中加载类,包括模型。假设类Application_Model_Hiring是在application/models/Hiring.php定义的,那么它应该可以工作。

更多信息请访问http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html