3

I'm attempting to create a new window when a user clicks a link in the navigation menu. What I'm attempting to do at present is just add a target="_blank" item to the url, thereby creating an entirely new page, and from there I'm planning to learn how to change this for my various other needs. The problem is, I'm unable to get the target to go to the associated link.

I've attempted:

$dropdown->addChild('Text', array('route' => 
     'routeName', 'routeParameters' => array('parmName' => 'parameter'), 
     'attr' => array('target' => '_blank'));

But the above results in it not adding the _blank to anything.

$dropdown->addChild('Text', array('route' => 'routeName',
     'routeParameters' => array('paramName' => 'parameter')))
             ->setAttribute('target', '_blank');

Results in the target being set to the li, not to the link itself, as seen below.

<li target="_blank" class="first">        <a href="routeLink">Text</a>

Is there a means to directly set the attribute to the link, so that it will open in a new window when clicked?

Any time and help you can provide is much appreciated.

4

2 回答 2

17

对于什么是值得的,如果你想在addChild方法上做到这一点:

$menu->addChild('Homepage', [
    'route' => 'homepage',
    'linkAttributes' => ['target' => '_blank'],
]);
于 2015-04-10T23:37:22.570 回答
12

如果您使用的是 KnpMenuBundle(看起来确实如此),您可以这样做:

$dropdown->setLinkAttributes(array('target' => '_blank'));
于 2013-09-23T16:00:20.783 回答