简体   繁体   中英

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. 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. So to keep a nice mvc-structure, I created 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

$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

<?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 . 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

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:-

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. Also the class is in the models folder, not modules. 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:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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