![](/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.