Zend不是自动加载模型

时间:2010-06-04 14:25:32

标签: php zend-framework autoloader

好的,这让我疯了!

我的目录结构如下:

application
- modules
-- default
--- controllers
--- models
---- DbTable
---- Cachmapper.php
--- views

我的配置文件看起来像这样

[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[] =

应用程序似乎有效,如果我导航到localhost,它会正确地转到索引控制器。但是,出于某种原因,它拒绝加载任何模型。

致命错误:在............................... / application / modules / default中找不到类'Model_Cachmapper'第26行的/controllers/IndexController.php

想法?

由于

4 个答案:

答案 0 :(得分:3)

我是新手ZF用户,所以这可能不是'正确',但我这样使用DB模型没有问题。 我的目录结构如下所示:

/
--/application
----/configs
----/controllers
----/forms
----/layouts
----/models
------/DbTable

在我的application / public / index.php中,我有以下代码:

...
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');
...

我的应用程序命名空间是Application(如果你不知道)。 我的数据库模型看起来像这样(位于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';
...
}

此外,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"

答案 1 :(得分:3)

这是ZF 1.10.x的工作版本(至少其中之一),也可能更早。

的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 - 相关部分

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"

并在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 :(得分:1)

将您的models目录添加到includePaths

我有:

includePaths.models = APPLICATION_PATH "/models"

我知道,你的模型属于你的模块,但应该可以做某种方式。也许在bootstrap而不是application.ini

Att:Rob

嗨Rob,

我很困惑。因为我实际上买了你的书“Zend Framework In Action”。

在源代码中“Places”应用的引导程序中,您有以下内容。

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

我知道源代码是为较旧的ZF版本编写的。我正在处理的当前应用程序基于Zend_Application,如果我从includePaths中删除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

答案 3 :(得分:1)

我会在模块中添加一个bootstrap类,否则模块加载器不会知道它。

class Default_Bootstrap extens Zend_Application_Module_Bootstrap
{
}

并存储在applications / default / Bootstrap.php

http://akrabat.com/modules还有更多信息。