从 Zend_Controller_Request 创建 Zend_Navigation_Page_Mvc 实例

发布于 2024-10-06 12:04:47 字数 954 浏览 0 评论 0原文

我的应用程序中有构成导航树的页面。我想使用请求的值动态地将页面插入到我的导航中。我已经有了找到该页面然后调用其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2024-10-13 12:04:47

我目前的资源有限,所以我无法真正检查我的建议,但是如果您在控制器中,为什么不……


$controller = $this->_request->getControllerName();
$action = $this->_request->getActionName();

$page = new Zend_Navigation_Page( array(
                   'label' => "Sonny's Page",
                   'controller' => $controller,
                   'action' => $action
));

也许您可以将其用作插件,以便您的视图超载?然后添加到您的导航容器中?

干杯,
天使

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..


$controller = $this->_request->getControllerName();
$action = $this->_request->getActionName();

$page = new Zend_Navigation_Page( array(
                   'label' => "Sonny's Page",
                   'controller' => $controller,
                   'action' => $action
));

Maybe you could use this as a plugin so that you overload your view? And then add to your Navigation Container?

Cheers,
Angel

铁轨上的流浪者 2024-10-13 12:04:47

由于我们不知道您已经编写的代码,所以我只是猜测...

您需要:

  • 检索 navigation() 视图帮助器中使用的实际 Zend_Navigation 容器
  • 从数据数组创建新的 Zend_Navigation_Page 实例从请求中检索
  • 将页面添加到容器
  • 将新容器分配给导航助手

这应该很容易。剩下的你需要知道:

  • 如何使用 preDispatch 方法编写控制器插件,并将上面的内容放在那里,
  • 如何访问此插件中的当前视图实例(从视图渲染器或从应用程序资源/引导程序)

然后在插件中,您可以像往常一样在视图中操作导航视图助手。

希望这能澄清一些事情。

Since we don't know the code you have written already, I'm only guessing…

You need to:

  • retrieve the actual Zend_Navigation container used in navigation() view helper
  • create new Zend_Navigation_Page instance from array of data retrieved from the request
  • add the page to the container
  • assign the new container to the navigation helper

This should be easy. The rest you need to know:

  • how to write controller plugin with preDispatch method, and put the above there,
  • how to access current view instance in this plugin (from the view renderer or from application resource/bootstrap)

Then in the plugin you operate on navigation view helper as usual in the view.

Hope this clarified some things.

浅唱ヾ落雨殇 2024-10-13 12:04:47

为什么不将实例存储在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文