开发者

How do I call the methods in a model via controller? Zend Framework

开发者 https://www.devze.com 2023-01-02 17:50 出处:网络
I\'ve been searching for tutorials to better understand this, but I\'m having no luck.Please forgive the lengthy explination, but I want make sure I explain myself well.

I've been searching for tutorials to better understand this, but I'm having no luck. Please forgive the lengthy explination, but I want make sure I explain myself well.

First, I'm quite new to the MVC structure, though I have been doing tutorials and learning as best I can.

I have been moving over a live site into the Zend Framework model. So far, I have all the views within views/scripts/index/example.phtml.

So therefore I'm using one IndexController and I have the code in each Action method for each page: IE public function exampleAction()

Because I didn't know how to interact with a model, I put all the methods at the bottom of the controller (a fat controller).

So basically, I had a working site by using a View and Controller and no model.

...

Now I'm trying to learn how to incorporate the Model.

So I created a View at:

view/scripts/calendar/index.phtml

I created a new Controller at:

controller/CalendarControllers.php

and a new model at:

model/Calendar.php

The problem is I think I'm not correctly communication with the model (I'm still new to OOP).

Can you look over my controller and model and tell me if you see a problem.

I'm needing to return an array from runCalendarScript(), but I'm not sure if I can return an array into the object like I'm trying to? I don't really understand how to "run" the runCalendarScript() from the controller?

Thanks for any help! I'm stripping out most of the guts of the methods for the sake of brevity:

controller:

<?php

class CalendarController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $finishedFeedArray = new Application_Model_Calendar(); 

  $this->view->googleArray = $finishedFeedArray;
    }
}

model:

   <?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);
        $eve开发者_Go百科ntData = $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;
 }
    }

Edit: sorry-I don't know why my formatting on the code keeps looking so ugly...


Joel, So you're putting the entire model object into a variable called $finishedFeedArray, that's going to get confusing (it's not an array, it's an object).

I think that's where your problem is. You're then trying to give this variable to your view, I assume to show values in your view. In your view script any attempt to treat this variable like an array is going to cause problems.

Try this:

class CalendarController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $calendar = new Application_Model_Calendar(); 

        $this->view->googleArray = $calendar->_runCalendarScript();
    }
}

There's a minor style issue there... I wouldn't name a public function with an underscore as the first character. Otherwise, this change should get at least one kink out of your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消