Zend 不自动加载模型

发布于 2024-09-04 19:37:59 字数 753 浏览 11 评论 0原文

好吧,这让我发疯!

我的目录结构如下:

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

该应用程序似乎可以工作,如果我导航到本地主机,它会正确地转到索引控制器。但是,由于某种原因它拒绝加载任何模型。

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

有想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

从来不烧饼 2024-09-11 19:37:59

这是一个适用于 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;
}
}

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;
}
}
流星番茄 2024-09-11 19:37:59

我是 ZF 的新手用户,所以这可能不“正确”,但我以这种方式使用数据库模型没有遇到任何问题。
我的目录结构如下所示:

/
--/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"

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"
南城旧梦 2024-09-11 19:37:59

我会向模块添加一个引导类,否则模块加载器不知道它。

class Default_Bootstrap extens Zend_Application_Module_Bootstrap
{
}

并存储在 applications/default/Bootstrap.php

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

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.

不念旧人 2024-09-11 19:37:59

将您的模型目录添加到您的 includePaths

我有:

includePaths.models = APPLICATION_PATH "/models"

我知道,您的模型位于您的模块下,但应该可以以某种方式执行。也许在引导程序中而不是 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 中删除模型文件夹,加载器将找不到我的模型。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文