我一直在尋找教程,以更好地理解這一點,但我沒有運氣。請原諒漫長的探索,但我想確保我解釋自己。如何通過控制器調用模型中的方法? Zend Framework
首先,我對MVC結構很陌生,儘管我一直在做教程和盡我所能學習。
我一直在將現場網站轉移到Zend Framework模型中。到目前爲止,我擁有views/scripts/index/example.phtml中的所有視圖。
因此,因此我使用一個索引控制器和我在每個頁面的每個動作方法的代碼:IE public function exampleAction()
因爲我不知道如何與模型交互,我把所有的方法在控制器的底部(一個胖控制器)。
所以基本上,我有一個工作站點通過使用視圖和控制器,沒有模型。
...
現在我正試圖學習如何合併模型。
所以我創建的視圖在:
view/scripts/calendar/index.phtml
我創建了一個新的控制器:
controller/CalendarControllers.php
,並在一個新的模式:
model/Calendar.php
的問題是,我認爲我與模特溝通不正確(我仍然是OOP的新手)。
你可以看看我的控制器和型號,並告訴我,如果你看到一個問題。
我需要從runCalendarScript()返回一個數組,但我不確定是否可以返回一個數組到我想要的對象?我真的不知道如何從控制器「運行」runCalendarScript()?
感謝您的幫助!我剝出最多的方法膽量爲簡潔起見:
控制器:
<?php
class CalendarController extends Zend_Controller_Action
{
public function indexAction()
{
$finishedFeedArray = new Application_Model_Calendar();
$this->view->googleArray = $finishedFeedArray;
}
}
型號:
<?php
class Application_Model_Calendar
{
public function _runCalendarScript(){
$gcal = $this->_validateCalendarConnection();
$uncleanedFeedArray = $this->_getCalendarFeed($gcal);
$finishedFeedArray = $this->_cleanFeed($uncleanedFeedArray);
return $finishedFeedArray;
}
//Validate Google Calendar connection
public function _validateCalendarConnection()
{
...
return $gcal;
}
//extracts googles calendar object into the $feed object
public function _getCalendarFeed($gcal)
{
...
return $feed;
}
//cleans the feed to just text, etc
protected function _cleanFeed($uncleanedFeedArray)
{
$contentText = $this->_cleanupText($event);
$eventData = $this->_filterEventDetails($contentText);
return $cleanedArray;
}
//Cleans up all formatting of text from Calendar feed
public function _cleanupText($event)
{
...
return $contentText;
}
//filterEventDetails
protected function _filterEventDetails($contentText)
{
...
return $data;
}
}
編輯:對不起,我不知道爲什麼我在代碼上的格式看起來很醜陋...
太棒了!非常感謝 - 真正教會了我如何將這一切融合在一起的重要教訓! – Joel 2010-06-07 04:21:02