zend framework model inclusion path configured perfectly and failed to load

廉价感情. 提交于 2019-12-11 19:06:29

问题


        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

and my index file is

<?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();

why iam getting this error i looked into loader.php at line 146 at that line there is

include_once($filename) so the error is originating from there


回答1:


There are quite a few problems here:

  • application/models, application/controllers and application/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

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;
}

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.

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.

More info at http://framework.zend.com/manual/1.12/en/zend.loader.autoloader-resource.html



来源:https://stackoverflow.com/questions/14331887/zend-framework-model-inclusion-path-configured-perfectly-and-failed-to-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!