Skip to content

Commit fc27444

Browse files
committed
Support the exact option, similar to vue-router
1 parent 68c0466 commit fc27444

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

spec/Menu/MenuGeneratorSpec.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,39 @@ function it_generates_submenu_items()
344344
$menu->getItems()->shouldReturn($items);
345345
}
346346

347+
function it_supports_the_exact_option_to_set_active_menu_items($url)
348+
{
349+
$url->current()->shouldBeCalled()->willReturn('http://example/contact/london');
350+
$url->to('')->shouldBeCalled()->willReturn('http://example/');
351+
352+
$url->to('contact', [], false)->shouldBeCalled()->willReturn('http://example/contact');
353+
$url->to('contact/london', [], false)->shouldBeCalled()->willReturn('http://example/contact/london');
354+
355+
$menu = $this->make([
356+
'contact' => ['url' => 'contact', 'exact' => true],
357+
'contact-london' => ['url' => 'contact/london'],
358+
]);
359+
360+
$menu->getItems()->shouldReturn([
361+
'contact' => [
362+
'class' => '',
363+
'submenu' => null,
364+
'id' => 'contact',
365+
'active' => false,
366+
'url' => 'http://example/contact',
367+
'title' => 'Contact',
368+
],
369+
'contact-london' => [
370+
'class' => 'active',
371+
'submenu' => null,
372+
'id' => 'contact-london',
373+
'active' => true,
374+
'url' => 'http://example/contact/london',
375+
'title' => 'Contact london',
376+
]
377+
]);
378+
}
379+
347380
function it_allows_you_to_set_a_custom_active_url_resolver()
348381
{
349382
$this->setActiveUrlResolver(function ($values) {

src/Menu/Menu.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected function generateItems($items, &$parentItem = null)
331331
unset(
332332
$values['callback'], $values['logged'], $values['roles'], $values['secure'],
333333
$values['params'], $values['route'], $values['action'], $values['full_url'],
334-
$values['allows'], $values['check'], $values['denies']
334+
$values['allows'], $values['check'], $values['denies'], $values['exact']
335335
);
336336
}
337337

@@ -368,12 +368,13 @@ protected function isActiveUrl(array $values)
368368
return $activeUrlResolver($values);
369369
}
370370

371-
// Otherwise use the default resolver:
372-
if ($values['url'] != $this->baseUrl) {
373-
return strpos($this->activeUrl, $values['url']) === 0;
371+
// If the current URL is the base URL or the exact attribute is set to true, then check for the exact URL
372+
if ($values['exact'] ?? false || $values['url'] == $this->baseUrl) {
373+
return $this->activeUrl === $values['url'];
374374
}
375375

376-
return $this->activeUrl === $this->baseUrl;
376+
// Otherwise use the default resolver:
377+
return strpos($this->activeUrl, $values['url']) === 0;
377378
}
378379

379380
/**

0 commit comments

Comments
 (0)