Zend_Navigation_Page_Mvc和片段标识符

时间:2011-05-10 19:50:42

标签: php zend-framework zend-navigation zend-framework-mvc

我在我的网站上使用Zend_Navigation。大多数项目为Zend_Navigation_Page_Mvc而非Zend_Navigation_Page_Uri。我知道我可以轻松地将fragment identifier添加到Zend_Navigation_Page_Uri项目,但这不是我正在寻找的优雅解决方案。 this post上有几个解决方案,但它们扩展了URL帮助程序,不会影响导航。

我已经使用了一个子类Zend_View_Helper_Navigation_Menu来渲染菜单。也许有一种方法可以通过htmlify()函数实现这一目标吗?

2 个答案:

答案 0 :(得分:1)

我最后在传递给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 . '>';
}

答案 1 :(得分:1)

linked to post中的解决方案不会影响导航,因为Zend_Navigation_Page_Mvc使用Zend_Controller_Action_Helper_Url而不是Zend_View_Helper_Url。您必须覆盖Zend_Controller_Action_Helper_Url :: url(),然后覆盖add your helper to the helper broker才能影响Zend_Navigation。

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

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>