Zend Framework:在索引视图中包括其他控制器
我是 Zend FW 的新手。我希望在名为 Feedparsercontroller's indexAction 的控制器中编写一个简单的 feedparser。但我想在我的索引页面上将解析后的提要输出显示为小部件。我如何将输出/变量数据驱动到我的索引视图?
下面是我的解析器。
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 am new to Zend FW. I am looking to write a simple feedparser in a controller named Feedparsercontroller'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我没有完全理解你的问题。
但是,这里最好的方法似乎是创建一个单独的提要控制器,该控制器单独负责与提要相关的业务逻辑(检索、按摩、设置查看等)。
然后,创建一个包含 javascript 代码的部分来调用 feed 控制器,然后输出您想要的小部件。这在一些事情上做得很好。
希望这会有所帮助!
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.
Hope this helps!
我认为小部件的最佳逻辑是ajax。
使用一些 js widgets 库(例如 jQuery ui),然后通过一些 ajax 查询加载这些小部件,返回 HTML,这允许您重新加载简单的小部件行为(无需重新加载整个页面) 。
在服务器端,您需要允许通过 ajax 请求调用控制器/操作,并仅发送 html 片段(而不是包含所有布局的整个页面)。
为此,请检查 ContextSwitch 和 AjaxContext 操作助手。您将告诉 FeedparserController 可以使用 XMLHHTTPRequest 中的 /format/html 调用索引操作,在这种情况下,视图助手将是索引。
在 init 部分,您会说可以在 ajax 模式下调用 indexAction,呈现 html 片段 ('html'):
现在只需将视图脚本
feedparser/index.phtml
重命名为feedparser/index .ajax.phtml
在indexAction中,做你的事情并在视图脚本上输出你想要的内容,不要考虑布局组合问题,你正在单独处理你自己的布局部分,并且组合是在js 端。
在 javascript 部分,确保您通过 ajax ($.load 或 $.ajax 与 jQuery 可能)调用带有 format/html 作为参数添加的 url(因此 http://example.com/feedparser/index/format/html)
请注意,在我看来,您应该使用 json 响应而不是 html,也许 json 带有一些里面的html。但这是一个关于如何控制 ajax 通信(以及处理错误、重定向等,这是另一个主题)的问题。
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'):
Now simply rename your view script
feedparser/index.phtml
tofeedparser/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).
视图助手怎么样?
您可以阅读查看 Zend Framework 中的助手
What about a view helper ?
You can read about it View Helpers in Zend Framework