![](/img/trans.png)
[英]How to call custom controller from index controller in Zend Framework
[英]Beginners Tutorial for Zend Framework implementing model and call it from Controller
我是ZF的新手,现在,我尝试编写一个基于ZF的小应用程序。 到目前为止,它或多或少都可以工作。 我想访问我的数据库数据。 对于初学者,我只想使用查询字符串,然后再开始使用zend_db混淆araound。 因此,为了保持良好的mvc结构,我创建了application / models / IndexMapper.php
class Application_Models_IndexMapper{...}
现在它只包含一个功能,看它是否有效
public function test(){
return ('yay');
}
在工作的IndexController中,我尝试通过以下方式访问模型
$indexMapper = new Application_Models_IndexMapper();
$x = $indexMapper->test();
但第一行抛出一个
Fatal error: Class 'Application_Models_IndexMapper' not found in /path/to/application/controllers/IndexController.php on line 31
由于我是新手,所以我不了解更复杂的教程,它们也无法帮助我解决问题。 我究竟做错了什么? 我是否必须以某种方式包含它?
谢谢
编辑:我的应用程序/ 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);
模型的结构可以在./application/models/IndexMapper.php
下./application/models/IndexMapper.php
。 在该文件中,您将拥有名为它的类,然后自动加载将起作用。
可以在www.akrabat.com上找到很棒的初学者教程。
您的课程在错误的位置,并且命名错误。
您的课程应该在application/models/Indexmapper.php
并且应该像这样:
class Application_Model_Indexmapper
{
public function test(){
return ('yay');
}
}
然后您这样称呼它:
$indexMapper = new Application_Model_Indexmapper();
$x = $indexMapper->test();
注意,我从模型末尾删除了“ s”,这不是必需的,并且会导致您发现的错误。 该类也位于models文件夹中,而不是模块中。 如果你想使用的模块,那么你需要阅读这和这从手动。
对于第一个基本项目,bootstrap.php应该看起来像这样:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//Yes, it's empty!
}
好吧,我想我的教程不是很有帮助。 我会按照建议进行操作,然后从头开始。 不过谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.