Last active
May 19, 2021 11:24
-
-
Save Slamdunk/4315c94b31429888e3cacab1979dc6a7 to your computer and use it in GitHub Desktop.
mezzio-laminasnavigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
// Config example | |
return [ | |
'routes' => [ | |
[ | |
'path' => '/index', | |
'middleware' => \My\IndexController::class, | |
'name' => \My\IndexController::class, | |
], | |
], | |
'navigation' => [ | |
'default' => [ | |
[ | |
'label' => 'Index', | |
'route' => \My\IndexController::class, | |
], | |
[ | |
'label' => 'View 123', | |
'route' => \My\IndexController::class, | |
'route_params' => ['action' => 'view'], | |
'query_params' => ['id' => 123], | |
], | |
[ | |
'label' => 'Custom URI', | |
'uri' => 'http://example.com', | |
], | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace My\Navigation; | |
use Laminas\Navigation\Page\AbstractPage; | |
use Mezzio\Helper\UrlHelper; | |
final class MezzioUriPage extends AbstractPage | |
{ | |
private UrlHelper $urlHelper; | |
private ?string $pagePath = null; | |
public function __construct( | |
UrlHelper $urlHelper, | |
$options, | |
) { | |
parent::__construct($options); | |
$this->urlHelper = $urlHelper; | |
} | |
public function getHref() | |
{ | |
if (null === $this->pagePath) { | |
\assert(isset($this->properties['route']) || isset($this->properties['uri'])); | |
$this->pagePath = $this->properties['uri'] | |
?? $this->urlHelper->generate( | |
$this->properties['route'], | |
$this->properties['route_params'] ?? [], | |
$this->properties['query_params'] ?? [], | |
$this->properties['fragment_identifier'] ?? null, | |
['reuse_result_params' => false] | |
) | |
; | |
} | |
return $this->pagePath; | |
} | |
public function isActive($recursive = false) | |
{ | |
if (! $this->active) { | |
if ($this->urlHelper->generate() === $this->getHref()) { | |
$this->active = true; | |
return true; | |
} | |
} | |
return parent::isActive($recursive); | |
} | |
public function toArray() | |
{ | |
return \array_merge( | |
parent::toArray(), | |
[ | |
'href' => $this->getHref(), | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment