简体   繁体   English

Zend Framework实现模型的初学者教程,并从Controller调用它

[英]Beginners Tutorial for Zend Framework implementing model and call it from Controller

I'm quite new to ZF, and right now, i try to write a tiny app, based on ZF. 我是ZF的新手,现在,我尝试编写一个基于ZF的小应用程序。 It works more or less fine until now. 到目前为止,它或多或少都可以工作。 I wanna access my db- data. 我想访问我的数据库数据。 For starters, i just want to use query-string, before I start messing araound with zend_db. 对于初学者,我只想使用查询字符串,然后再开始使用zend_db混淆araound。 So to keep a nice mvc-structure, I created application/models/IndexMapper.php 因此,为了保持良好的mvc结构,我创建了application / models / IndexMapper.php

class Application_Models_IndexMapper{...}

it just contains one function by now to see if it works 现在它只包含一个功能,看它是否有效

 public function test(){
    return ('yay');
}

In my IndexController, which is working, i try to access my model by 在工作的IndexController中,我尝试通过以下方式访问模型

$indexMapper = new Application_Models_IndexMapper();
$x = $indexMapper->test();

but the first line throws an 但第一行抛出一个

Fatal error: Class 'Application_Models_IndexMapper' not found in /path/to/application/controllers/IndexController.php on line 31

As I'm new, I don't understand the more complex tutorials and they don't help me fix my problem. 由于我是新手,所以我不了解更复杂的教程,它们也无法帮助我解决问题。 What am I doing wrong? 我究竟做错了什么? Do I have to include it somehow? 我是否必须以某种方式包含它?

Thanks 谢谢

edit: my application/bootstrap.php 编辑:我的应用程序/ bootstrap.php

<?php

defined('APPLICATION_PATH')
    or define('APPLICATION_PATH' , dirname(__FILE__));


defined('APPLICATION_ENVIRONMENT')
    or define('APPLICATION_ENVIRONMENT' , 'development');


require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();


$frontController = Zend_Controller_Front::getInstance();

$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');

$frontController->setParam('env', APPLICATION_ENVIRONMENT);

Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');


//Doctype
$view = Zend_Layout::getMvcInstance()->getView();
$view->doctype('HTML5');

$view->addHelperPath('App/View/Helper', 'App_View_Helper');


unset($frontController);

The structure for a model would be found under ./application/models/IndexMapper.php . 模型的结构可以在./application/models/IndexMapper.php./application/models/IndexMapper.php In that file you would have the class As you named it and then the autoloading will work. 在该文件中,您将拥有名为它的类,然后自动加载将起作用。

A great beginner tutorial would be found on www.akrabat.com 可以在www.akrabat.com上找到很棒的初学者教程。

You have your class in the wrong place and have named it incorrectly. 您的课程在错误的位置,并且命名错误。

Your class should be in application/models/Indexmapper.php and should look like this:- 您的课程应该在application/models/Indexmapper.php并且应该像这样:

class Application_Model_Indexmapper
{
    public function test(){
        return ('yay');
    }
}

Then you call it thus:- 然后您这样称呼它:

$indexMapper = new Application_Model_Indexmapper();
$x = $indexMapper->test();

Notice I dropped the 's' from the end of Models, it is not required and will cause an error as you found. 注意,我从模型末尾删除了“ s”,这不是必需的,并且会导致您发现的错误。 Also the class is in the models folder, not modules. 该类也位于models文件夹中,而不是模块中。 If you want to use modules then you need to read this and this from the manual. 如果你想使用的模块,那么你需要阅读从手动。

Your bootstrap.php should look like this for a first, basic project:- 对于第一个基本项目,bootstrap.php应该看起来像这样:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    //Yes, it's empty!
}

Well, I guess my tutorial wasn't very helpful. 好吧,我想我的教程不是很有帮助。 I'll do as recommended, and start over from scratch. 我会按照建议进行操作,然后从头开始。 Thanks though 不过谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM