如何编写正文中有锚标记的 Zend 框架 URL? [英] How Can I Write Zend Framework URLs That Have Anchor Tags In The Body?

查看:24
本文介绍了如何编写正文中有锚标记的 Zend 框架 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Zend Framework 中设置的标准 MVC,我希望能够显示始终具有锚点的页面.现在,我只是在 .phtml 文件中添加一个带有#anchor"的无意义参数.

Using the standard MVC set up in Zend Framework, I want to be able to display pages that have anchors throughout. Right now I'm just adding a meaningless parameter with the '#anchor' that I want inside the .phtml file.

<?= $this->url(array(
    'controller'=>'my.controller',
    'action'=>'my.action',
    'anchor'=>'#myanchor'
));

这会将 URL 设置为/my.controller/my.action/anchor/#myanchor

This sets the URL to look like /my.controller/my.action/anchor/#myanchor

有没有更好的方法来实现这一点?导航到锚链接后,额外的项目参数设置在用户的 URL 中,这是我不希望发生的事情.

Is there a better way to accomplish this? After navigation to the anchor link, the extra item parameter gets set in the user's URL which is something I would rather not happen.

推荐答案

一种可能性是覆盖 url helper,或者创建一个新的.

one of possibilities is to override url helper, or to create a new one.

class My_View_Helper_Url extends Zend_View_Helper_Url
{    
    public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
    {
        if (isset($urlOptions['anchor']) && !empty($urlOptions['anchor']))
        {
            $anchor = $urlOptions['anchor'];
            unset($urlOptions['anchor']);
        }
        else
        {
            $anchor = '';
        }

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

这个助手覆盖了url助手,问题是你不能使用名为'anchor'的参数,因为它会在url中变成锚点.

this helper override url helper, problem is, that you can't use parameter called 'anchor', because it will be changed into anchor in url.

你会像在你的例子中那样称呼它

you will call it as in your's example

<?= $this->url(array(
    'controller'=>'my.controller',
    'action'=>'my.action',
    'anchor'=>'#myanchor'
));

希望能帮到你

这篇关于如何编写正文中有锚标记的 Zend 框架 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆