从 Zend_Controller_Request 创建 Zend_Navigation_Page_Mvc 实例
我的应用程序中有构成导航树的页面。我想使用请求的值动态地将页面插入到我的导航中。我已经有了找到该页面然后调用其 addPage()
方法的逻辑。我正在寻找的是如何轻松地将 Zend_Controller_Request
值传递给 Zend_Navigation_Page::factory()
以便我可以添加该页面。甚至可能写成插件?
解决方案
AngelP 最接近,所以我给了他荣誉,但这是我的解决方案:
$request = $this->getRequest();
if ($page = $this->view->siteNav->findBy('id', $page_id)) {
$page->addPage(Zend_Navigation_Page::factory($request->getParams())
->setParams($request->getParams())
->setLabel($this->view->title)
->setVisible(false));
}
此代码是从控制器操作执行的。 $this->view->siteNav
是我在视图中的 Zend_Navigation
的实例。来自 Zend_Controller_Request
实例的 getParams()
可以轻松传递到 Zend_Navigation_Page::factory()
,然后传递到 setParams()
Zend_Navigation_Page_Mvc
实例的 code> 方法。
I have pages in my application that make up the navigation tree. I would like to dynamically insert pages into my navigation using the values of the request. I already have the logic to find the page and then call the addPage()
method on it. What I'm looking for is how to easily pass the Zend_Controller_Request
values to Zend_Navigation_Page::factory()
so I can add that page. Maybe even written as a plugin?
Solution
AngelP got the closest, so I'm giving him credit, but here's my solution:
$request = $this->getRequest();
if ($page = $this->view->siteNav->findBy('id', $page_id)) {
$page->addPage(Zend_Navigation_Page::factory($request->getParams())
->setParams($request->getParams())
->setLabel($this->view->title)
->setVisible(false));
}
This code is executed from a controller action. $this->view->siteNav
is an instance of Zend_Navigation
that I have in the view. getParams()
from the Zend_Controller_Request
instance is easily passed to Zend_Navigation_Page::factory()
and then to the setParams()
method of the Zend_Navigation_Page_Mvc
instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我目前的资源有限,所以我无法真正检查我的建议,但是如果您在控制器中,为什么不……
也许您可以将其用作插件,以便您的视图超载?然后添加到您的导航容器中?
干杯,
天使
I have limited resources at the moment so I cannot really check my suggestion, but if you're in you controller, why don't you..
Maybe you could use this as a plugin so that you overload your view? And then add to your Navigation Container?
Cheers,
Angel
由于我们不知道您已经编写的代码,所以我只是猜测...
您需要:
navigation()
视图帮助器中使用的实际 Zend_Navigation 容器这应该很容易。剩下的你需要知道:
preDispatch
方法编写控制器插件,并将上面的内容放在那里,然后在插件中,您可以像往常一样在视图中操作导航视图助手。
希望这能澄清一些事情。
Since we don't know the code you have written already, I'm only guessing…
You need to:
navigation()
view helperThis should be easy. The rest you need to know:
preDispatch
method, and put the above there,Then in the plugin you operate on navigation view helper as usual in the view.
Hope this clarified some things.
为什么不将实例存储在 Zend_Registry 中,然后在 postDispatch 中从插件、模块引导程序或操作控制器将页面添加到原始导航?
Why not store the instance in Zend_Registry and then in a postDispatch from either a plugin, module bootstrap or action controller add the pages to the original nav?