Zend_Navigation_Page_Mvc 和片段标识符

发布于 2024-11-06 20:12:41 字数 596 浏览 4 评论 0原文

我在我的网站上使用 Zend_Navigation。大多数项目是 Zend_Navigation_Page_Mvc 而不是 Zend_Navigation_Page_Uri。我知道我可以轻松添加 片段标识符 到 Zend_Navigation_Page_Uri 项,但这不是我正在寻找的优雅解决方案。 这篇文章,但它们扩展了 URL 帮助器并且不影响导航。

我已经使用子类 Zend_View_Helper_Navigation_Menu 来呈现菜单。也许有一种方法可以使用 htmlify() 函数来完成此任务?

I use Zend_Navigation on my site. Most of the items are Zend_Navigation_Page_Mvc and NOT Zend_Navigation_Page_Uri. I know that I can easily add a fragment identifier to a Zend_Navigation_Page_Uri item, but that's not the elegant solution I'm looking for. There are a couple of solutions on this post, but they extend the URL helper and don't affect navigation.

I already use a sub-classed Zend_View_Helper_Navigation_Menu to render the menus. Maybe there's a way to accomplish this with the htmlify() function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你丑哭了我 2024-11-13 20:12:41

我最终将一个名为 fragment 的属性添加到传递给 Zend_Navigation 的页面数组,然后在我的子类 Zend_View_Helper_Navigation_Menu::htmlify().这是我的 htmlify() 代码:

/**
 * Returns an HTML string containing an 'a' element for the given page if
 * the page's href is not empty, and a 'span' element if it is empty
 *
 * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}.
 *
 * @param  Zend_Navigation_Page $page  page to generate HTML for
 * @return string                      HTML string for the given page
 */
public function htmlify(Zend_Navigation_Page $page)
{
    // get label and title for translating
    $label = $page->getLabel();
    $title = $page->getTitle();

    // translate label and title?
    if ($this->getUseTranslator() && $t = $this->getTranslator()) {
        if (is_string($label) && !empty($label)) {
            $label = $t->translate($label);
        }
        if (is_string($title) && !empty($title)) {
            $title = $t->translate($title);
        }
    }

    // get attribs for element
    $attribs = array(
        'id'     => $page->getId(),
        'title'  => $title,
        'class'  => $page->getClass()
    );

    // does page have a href?
    if ($href = $page->getHref()) {
        $element = 'a';
        $attribs['href'] = $href;
        $attribs['target'] = $page->getTarget();
        // fragment identifier
        if (0 < strlen($fragment = $page->get('fragment'))) {
            $attribs['href'] .= '#' . $fragment;
        }
    } else {
        $element = 'span';
    }

    return '<' . $element . $this->_htmlAttribs($attribs) . '><span>'
         . $this->view->escape($label)
         . '</span></' . $element . '>';
}

I ended up adding a property called fragment to the page array passed into Zend_Navigation and then checking for it in my sub-classed Zend_View_Helper_Navigation_Menu::htmlify(). Here is my htmlify() code:

/**
 * Returns an HTML string containing an 'a' element for the given page if
 * the page's href is not empty, and a 'span' element if it is empty
 *
 * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}.
 *
 * @param  Zend_Navigation_Page $page  page to generate HTML for
 * @return string                      HTML string for the given page
 */
public function htmlify(Zend_Navigation_Page $page)
{
    // get label and title for translating
    $label = $page->getLabel();
    $title = $page->getTitle();

    // translate label and title?
    if ($this->getUseTranslator() && $t = $this->getTranslator()) {
        if (is_string($label) && !empty($label)) {
            $label = $t->translate($label);
        }
        if (is_string($title) && !empty($title)) {
            $title = $t->translate($title);
        }
    }

    // get attribs for element
    $attribs = array(
        'id'     => $page->getId(),
        'title'  => $title,
        'class'  => $page->getClass()
    );

    // does page have a href?
    if ($href = $page->getHref()) {
        $element = 'a';
        $attribs['href'] = $href;
        $attribs['target'] = $page->getTarget();
        // fragment identifier
        if (0 < strlen($fragment = $page->get('fragment'))) {
            $attribs['href'] .= '#' . $fragment;
        }
    } else {
        $element = 'span';
    }

    return '<' . $element . $this->_htmlAttribs($attribs) . '><span>'
         . $this->view->escape($label)
         . '</span></' . $element . '>';
}
柒夜笙歌凉 2024-11-13 20:12:41

linked to post 不会影响导航,因为 Zend_Navigation_Page_Mvc 使用 Zend_Controller_Action_Helper_Url 而不是 Zend_View_Helper_Url。您必须覆盖 Zend_Controller_Action_Helper_Url::url() 然后 将您的助手添加到助手代理 以使其影响 Zend_Navigation。

在我的实现中,我使用 '_fragment' 选项/参数来帮助防止名称冲突:

require_once('Zend/Controller/Action/Helper/Url.php');
class My_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Url
{
    public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
    {
        $fragment = '';
        if(isset($urlOptions['_fragment'])) {
            $fragment = '#' . $urlOptions['_fragment'];
            unset($urlOptions['_fragment']);
        }

        return parent::url($urlOptions, $name, $reset, $encode) . $fragment;
    }
}

然后,在我用来配置 Zend_Navigation 的 XML 文件中,我创建一个 _fragment 参数:

<page>
    <id>pageId</id>
    <label>Page Label</label>
    <module>foo</module>
    <params>
        <_fragment>bar</_fragment>
    </params>
    <route>default</route>
</page>

The solutions in the linked to post don't affect navigation because Zend_Navigation_Page_Mvc uses Zend_Controller_Action_Helper_Url and not Zend_View_Helper_Url. You have to override Zend_Controller_Action_Helper_Url::url() and then add your helper to the helper broker for it to affect Zend_Navigation.

In my implementation, I use a '_fragment' option/param to help prevent name conflicts:

require_once('Zend/Controller/Action/Helper/Url.php');
class My_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Url
{
    public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
    {
        $fragment = '';
        if(isset($urlOptions['_fragment'])) {
            $fragment = '#' . $urlOptions['_fragment'];
            unset($urlOptions['_fragment']);
        }

        return parent::url($urlOptions, $name, $reset, $encode) . $fragment;
    }
}

Then, in the XML file I use to configure Zend_Navigation, I create a _fragment param:

<page>
    <id>pageId</id>
    <label>Page Label</label>
    <module>foo</module>
    <params>
        <_fragment>bar</_fragment>
    </params>
    <route>default</route>
</page>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文