Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 694
mazniak Avatar asked Jan 01 '09 19:01

mazniak


1 Answers

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

like image 149
Martin Rázus Avatar answered Oct 03 '22 23:10

Martin Rázus