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
包含路径中的
in the inclusion path
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();
为什么我得到这个错误我在第146行看到了loader.php
why iam getting this error i looked into loader.php at line 146 at that line there is
include_once($ filename)
所以错误来自那里
推荐答案
这里有很多问题:
-
申请/ models
,application / controllers
和application / views / scripts
不应该在包括路径。 -
$ loader-> registerNamespace('hiring');
应该是$ loader-> registerNamespace('Hiring _');
(尽管代码示例中没有任何符号表明您正在使用此命名空间)。 -
$ loader-> setFallbackAutoloader(true);
可能不需要(包含的代码示例中没有任何符号)你需要这个)。 - 应删除所有
Zend_Loader :: loadClass
行。自动装带器的重点在于您不需要自己要求或自己加载类 - 至少应将前端控制器配置移至您的引导类
application/models
,application/controllers
andapplication/views/scripts
should not be on the include path.$loader->registerNamespace('hiring');
should probably be$loader->registerNamespace('Hiring_');
(although there's no sign in the code sample you've included that you are using this namespace).$loader->setFallbackAutoloader(true);
is probably not needed (there's no sign in the code sample you've included that you need this).- All of the
Zend_Loader::loadClass
lines should be removed. The whole point of an autoloader is that you don't need to then require in or load classes yourself - At least the front controller configuration should be moved to your bootstrap class
但这些都不会影响您报告的问题。标准自动加载器设置只会加载其名称可以直接映射到文件系统的类(通过将下划线转换为路径中的斜杠,例如类 Zend_Db_Table
将存在于库/ Zend的/ DB / Table.php
)。类 Application_Model_Hiring
类不适合这个模型,如果你想使用这个命名方案,你还需要设置一个资源自动加载器,它只映射类的最后一部分命名为 application /
中的一些预定义子文件夹。
But none of these things will affect the problem you are reporting. The standard autoloader setup will only load classes whose name can be mapped directly to the file system (by converting underscores to slashes in the path, e.g. the class Zend_Db_Table
would live at library/Zend/Db/Table.php
). The class Application_Model_Hiring
does not fit this model, if you want to use this naming scheme as well you need to also setup a resource autoloader, which maps just the last part of the class name to some pre-defined sub-folders within application/
.
将以下方法添加到引导类:
Add the following method to your bootstrap class:
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,只有在库文件夹中包含以此名称开头的类时才需要它。
This setups up the standard autoloader ($autoloader = Zend_Loader_Autoloader::getInstance();
), which will autoload the Zend Framework classes. It then registers a namespace 'Hiring', which you only need if you are including classes that start with this name in your library folder.
然后创建一个单独的资源自动加载器使用命名空间Application,它将从应用程序文件夹中加载类,包括模型。假设类 Application_Model_Hiring
在 application / models / Hiring.php
中定义,那么它应该可以工作。
It then creates a separate resource autoloader with the namespace 'Application', which will load classes from the application folder, including models. Assuming the class Application_Model_Hiring
is defined at application/models/Hiring.php
it should then work.
更多信息,请访问 http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html
这篇关于zend框架模型包含路径配置完美且无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!