开发者

Zend Framework : Incuding other controllers in index view

I am new to Zend FW. I am looking to write a simple feedparser in a controller named Feedpars开发者_如何学JAVAercontroller's indexAction. but i want to display the parsed feed output as a widget on my index page. how can i drive the output/ variable data to my indexview?

The below is my parser.

class FeedparserController extends Zend_Controller_Action {

public function init() {
    /* Initialize action controller here */
}

public function indexAction() {

    $feedUrl = 'http://feeds.feedburner.com/ZendScreencastsVideoTutorialsAboutTheZendPhpFrameworkForDesktop';
    $feed = Zend_Feed_Reader::import ( $feedUrl );

    $this->view->gettingStarted = array ();
    foreach ( $feed as $entry ) {
        if (array_search ( 'Getting Started', $entry->getCategories ()->getValues () )) {
            $this->view->gettingStarted [$entry->getLink ()] = $entry->getTitle ();
        }
    }
}

}

i want to implement the same with my login , register controllers as well.


Perhaps I'm not understanding your question fully.

But, it seems the best approach here would be to create a separate feed controller that is solely responsible for the business logic associated with feeds (retrieving, massaging, setting to view, etc).

Then, create a partial which contains javascript code to call the feed controller, which then outputs the widget you're desiring. This does a few things very well.

  1. It centralizes feed-related logic
  2. It allows you to place the feed widget wherever you want
  3. It is a SOA approach which is generally a good thing

Hope this helps!


I think the best logic with widgets is ajax.

Use some js widgets libraries (maybe jQuery ui for example), then make these widgets be loaded by some ajax queries, returning HTML, this allow you as well simple widgets reloading behviours (without relaoding the whole page).

In the server Side you'll need to allow your controller/Action to be called via ajax requests and to send only html snippets (not a whole page with all the layout).

To do that check ContextSwitch and AjaxContext Action Helpers. You will tell your FeedparserController that the index action can be called with /format/html in an XMLHHTTPRequest, and that in this case the view helper will be index.

In the init part you will say the indexAction can be called in ajax mode, rendering html snippets ('html'):

$Ajaxcontext = $this->_helper->getHelper('AjaxContext');
$Ajaxcontext->addActionContext('index', 'html')
            ->initContext();

Now simply rename your view script feedparser/index.phtml to feedparser/index.ajax.phtml

In the indexAction, do your stuff and output what you want on your view script, do not think about layout composition problems, you're working alone with your own layout part and the composition is done on the js side.

In the javascript part, ensure you're calling via ajax ($.load or $.ajax with jQuery maybe) the url with format/html added as parameters (so http://example.com/feedparser/index/format/html)

Note that in my opinion you should use json responses and not html, maybe json with some html inside. But that's a matter on how you want to control your ajax communication (and handle errors, redirection and such, and it's another subject).


What about a view helper ?

You can read about it View Helpers in Zend Framework

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜