我一直在寻找教程,以更好地理解这一点,但我没有运气。请原谅漫长的探索,但我想确保我解释自己。如何通过控制器调用模型中的方法? 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