How to call methods in a model through a controller? Zend framework

I was looking for textbooks to better understand this, but I was out of luck. Please forgive the long explanation, but I want to make sure I explain well.

Firstly, I am fairly new to the MVC framework, although I did the training and studied as best as possible.

I navigate a live site into the Zend Framework model. So far, I have all the views in views / scripts / index / example.phtml.

Therefore, I use one IndexController, and I have code in each Action method for each page: IE public function exampleAction()

Since I did not know how to interact with the model, I put all the methods at the bottom of the controller (fat controller).

So basically I had a working site using View and Controller and without a model.

...

Now I am trying to learn how to enable the model.

So, I created View at:

 view/scripts/calendar/index.phtml 

I created a new controller at:

 controller/CalendarControllers.php 

and a new model with:

 model/Calendar.php 

The problem is that I think that I am not communicating correctly with the model (I am still new to OOP).

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

I need to return an array from runCalendarScript (), but I'm not sure if I can return the array to an object, how am I trying? I really don't understand how to β€œrun” runCalendarScript () from a controller?

Thanks for any help! I cut most of the method brushes 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); $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; } } 

Edit: Sorry, I don't know why my code formatting continues to look so ugly ...

+4
source share
1 answer

Joel, So you put the whole model object in a variable called $ finishedFeedArray, which will be confusing (this is not an array, this is an object).

I think where your problem is. Then you try to pass this variable to your view, I assume that you are displaying values ​​in your view. In your view of the script, any attempt to process this variable, similar to an array, will cause problems.

Try the following:

 class CalendarController extends Zend_Controller_Action { public function indexAction() { $calendar = new Application_Model_Calendar(); $this->view->googleArray = $calendar->_runCalendarScript(); } } 

There is a problem with a little style ... I would not name a public function with an underscore as the first character. Otherwise, this change should output at least one bend of your code.

+2
source

Source: https://habr.com/ru/post/1312043/


All Articles