问题
Ok, this is driving me nuts!
I have a directory structure as follows:
application
- modules
-- default
--- controllers
--- models
---- DbTable
---- Cachmapper.php
--- views
My config file looks like this
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
The application seems to work, if I navigate to localhost, it correctly goes to the index controller. But, for some reason it refuses to load any models.
Fatal error: Class 'Model_Cachmapper' not found in .............................../application/modules/default/controllers/IndexController.php on line 26
Ideas?
Thanks
回答1:
Here's a working version (one of them, at least), for ZF 1.10.x and probably earlier, too.
index.php
<?php
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// 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(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
application.ini - the relevant parts
autoloadernamespaces[] = Zend_
includePaths.library = APPLICATION_PATH "/../library"
; Where will Zend_Application find the Bootstrap file
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
; Where are all the modules
resources.frontcontroller.moduledirectory = APPLICATION_PATH"/modules"
resources.modules[] = ""
; And which is the default module
resources.frontcontroller.defaultmodule = "default"
and in application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
/**
* Autoloader for the "public" module
*
* @return Zend_Application_Module_Autoloader
*/
public function _initPublicAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'
)
);
return $moduleLoader;
}
}
回答2:
I am a novice ZF user, so this may not be 'correct' but I have had no problems using DB Models this way. My directory structure looks like this:
/
--/application
----/configs
----/controllers
----/forms
----/layouts
----/models
------/DbTable
In my application/public/index.php I have the following code:
...
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Application_');
require_once 'Zend/Loader/Autoloader/Resource.php';
$resources = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH
));
$resources->addResourceType('form','forms','Form');
$resources->addResourceType('model','models','Model');
$resources->addResourceType('dbtable','models/DbTable','Model_DbTable');
...
My application namespace is Application (if you couldn't tell). My database models look like this (located in application/models/DbTable:
class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
protected $_name = 'User';
protected $_primary = 'username';
}
class Application_Model_DbTable_WinLossTieScore extends Zend_Db_Table_Abstract
{
protected $_name = 'WinLossTieScore';
protected $_primary = 'id';
...
}
Also, the relevant portions of application/configs/application.ini:
appnamespace = "Application"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
回答3:
I would add a bootstrap class to the module as otherwise the module loader doesn't know about it.
class Default_Bootstrap extens Zend_Application_Module_Bootstrap
{
}
And store in applications/default/Bootstrap.php
http://akrabat.com/modules has some more info.
回答4:
Add your models directory to your includePaths
I have:
includePaths.models = APPLICATION_PATH "/models"
I know, your models lies under your modules, but should be possible to do somehow. Maybe in the bootstrap instead of application.ini
Att.: Rob
Hi Rob,
Well i'm confused. Because i actually bought your book 'Zend Framework In Action'.
And in the bootstrap of the 'Places' app in the sourcecode, you have the following.
set_include_path(get_include_path()
. PATH_SEPARATOR . ROOT_DIR . '/library/'
. PATH_SEPARATOR . ROOT_DIR . '/application/models/'
. PATH_SEPARATOR . ROOT_DIR . '/application/forms/'
);
include 'Zend/Loader.php';
Zend_Loader::registerAutoload();
I know that the source is written for an older ZF version. My current app i'm working on is based on Zend_Application, and if i remove the models folder from my includePaths, the loader can't find my models.
Warning: include_once(Users.php) [function.include-once]: failed to open stream: No such file or directory in C:\Programmer\wamp\include\Zend\Loader.php on line 146
来源:https://stackoverflow.com/questions/2974996/zend-not-autoloading-models