如何编写在正文中包含锚标记的 Zend Framework URL?

发布于 2024-07-10 16:34:09 字数 381 浏览 5 评论 0原文

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

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

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

有更好的方法来完成此操作吗? 导航到锚链接后,额外的项目参数会在用户的 URL 中设置,这是我不希望发生的事情。

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'
));

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

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.

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

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

发布评论

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

评论(4

与风相奔跑 2024-07-17 16:34:09

一种可能性是覆盖 url 帮助程序,或创建一个新的。

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->url(array(
    'controller'=>'my.controller',
    'action'=>'my.action',
    'anchor'=>'#myanchor'
));

我希望它有帮助

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;
    }
}

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'
));

I hope it helps

撩心不撩汉 2024-07-17 16:34:09

有多种方法可以实现 片段 id< /a> 进入您的网址。 以下是一些选项,以及每个选项的优点和缺点。

直接添加

您只需在 url() 调用后添加 "#$fragment_id" 即可。 不优雅,但简单。 如果您不经常使用页面锚点(即仅一两个页面),那么这就是正确的方法。

编写自定义 url() 帮助程序

您可以编写自定义版本的 url() ,为片段 id 附加可选的第五个参数:

class My_View_Helper_Url extends Zend_View_Helper_Url
{    
    public function url(array $urlOptions  = array(), $name   = null, 
                              $reset       = false,   $encode = true, 
                              $fragment_id = null)
    {
        $uri = parent::url($urlOptions, $name, $reset, $encode);

        if(!is_null($fragment_id)) {
            $uri .= "#$fragment_id";
        }

        return $uri;
    }
}

这样,锚点(以及锚点/片段id) 信息严格保存在视图的范围内。 这对于一般用途来说很好,但对于默认路由来说可能有点笨拙。 此外,对于某些用途来说,这仍然有点过于硬编码。

编写自定义 Route 类(极端)

作为第三个选项,您可以编写 Zend_Controller_Router_Route 类的自定义版本,特别是 assemble($data , $reset, $encode) 方法(match($path) 方法默认忽略片段 ID)。

使用此方法可能非常棘手,但非常有用,特别是如果使用仅限于特定路由(此方法可用于将片段 id 建立在任何变量的基础上)。

警告

使用片段 ID 时必须考虑某些注意事项。 例如,查询字符串必须位于 uri 中的片段 id 之前,否则 PHP 会忽略查询字符串。 但是,大多数 ZF 应用程序倾向于避免使用查询字符串,因此这可能不是问题。

There are multiple ways you could go about implementing a fragment id into your URLs. Below are some options, along with some pros and cons for each.

Direct Add

You could simply add the "#$fragment_id" after your url() call. Inelegant, but simple. If you don't use page anchors much (i.e. One or two pages only), this is the way to go.

Write a custom url() helper

You could write a custom version of url() appending an optional 5th argument for the fragment id:

class My_View_Helper_Url extends Zend_View_Helper_Url
{    
    public function url(array $urlOptions  = array(), $name   = null, 
                              $reset       = false,   $encode = true, 
                              $fragment_id = null)
    {
        $uri = parent::url($urlOptions, $name, $reset, $encode);

        if(!is_null($fragment_id)) {
            $uri .= "#$fragment_id";
        }

        return $uri;
    }
}

This way, anchor (and anchor/fragment id) information is kept strictly withing the realm of the View. This is good for general use, but can get a little unwieldy for the default route. Also, this is still a little too hard-coded for some uses.

Write a custom Route class (Extreme)

As a third option, you could write a custom version of the Zend_Controller_Router_Route class(es), specifically the assemble($data, $reset, $encode) method (the match($path) method ignores fragment ids by default).

Using this method can be quite tricky, but very useful, especially if use is only limited to specific routes (this method can be used to base the fragment id off of any variable).

Caveat

Certain considerations must be taken into account when using fragment ids. For example, query strings have to precede the fragment id in the uri, otherwise, the query string ignored by PHP. However, most ZF applications tend to avoid use of query strings, so it may not be an issue.

深白境迁sunset 2024-07-17 16:34:09

url 视图助手接受第三个选项的“fragment”键:

url('[route]',array([params]),array('fragment'=>'anchor'));

这将自动以 #anchor 结束 url。

-感谢Exlord

The url view helper accepts a 'fragment' key for the third option:

url('[route]',array([params]),array('fragment'=>'anchor'));

this will automatically end the url with #anchor.

-Thanks to Exlord

我不吻晚风 2024-07-17 16:34:09

我认为编写自定义路由类的极端方法更好,因为其他帮助器将具有相同的行为(例如重定向器操作帮助器)。

I think the Extreme method of writing a custom route class is better because other helper will have the same behavior (like the redirector action helper).

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