开发者

How do I pass data from Controller Foo to an View in Controller Bar?

In zend I'm interested in passing data from one controller to a view in another controller.

For example, I have a search controller whose purpose is to use the search model to query SOLR, and I want to have Index Controller call it from the view (via ajax or other means) a开发者_Go百科nd have Search controller return data to that view. Is that feasible? And is there a better way to do it?

Thanks in advance.


If you want to do it by AJAX, then you just need to have your client-side script (javascript, I'm guessing) initiate a request to the url corresponding to the search you wish to perform. You'd probably configure the search controller/action to use a context-switch so that it delivers the results in a format (json or xml, I'm guessing) in a format consumable by your client-side code.

The downsides to this approach include: making two full requests, including the HTTP overhead, to get the data; you'll get nothing for clients without js.

An alternative is let the index view-script call the Action view helper - called using $this->action($action, $controller, $module, $params) - to get the data directly. Again, you might wish to employ a context-switch or a param flag so that you don't deliver the HTML for the full site layout, but only that of the search results.

Finally, you could push that search functionality down into a service class so that both the Index controller and the Search controller can avail themselves of it independently. Then the controller just assigns those results to the view in the normal manner.

Personally, I tend to go for the service approach.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜