How Can I Write Zend Framework URLs That Have Anchor Tags In The Body?
Asked Answered
D

4

11

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.

Detour answered 1/1, 2009 at 19:20 Comment(1)
Could you please post what is your desired result URL? your description is pretty hard to understandRepro
H
13

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

Hailstorm answered 2/1, 2009 at 12:55 Comment(1)
What about using # as key instead of anchor to fix the problem mentioned ?Planogamete
U
7

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.

Untold answered 24/1, 2009 at 2:41 Comment(0)
D
5

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

Denunciation answered 30/7, 2014 at 15:34 Comment(2)
This solution fits zf2 only, doesn't it?Natalee
ZF2 +1 vote for me. I use this method in ZF2 projects. see framework.zend.com/manual/current/en/modules/zend.view.helpers.url.htmlKilligrew
M
0

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

Matheson answered 24/2, 2009 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.